Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed May 16, 2019
1 parent 71ecc82 commit c25465e
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 320 deletions.
22 changes: 7 additions & 15 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ The data structures mainly includes:
- [0055.jump-game](./problems/55.jump-game.md) 🆕
- [0062.unique-paths](./problems/62.unique-paths.md )🆕
- [0075.sort-colors](./problems/75.sort-colors.md)
- [0078.subsets](./problems/78.subsets.md) 🆕
- [0078.subsets](./problems/78.subsets.md)
- [0086.partition-list](./problems/86.partition-list.md)
- [0090.subsets-ii](./problems/90.subsets-ii.md)
- [0091.decode-ways](./problems/91.decode-ways.md) 🆕
Expand All @@ -142,21 +142,23 @@ The data structures mainly includes:
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md) 🆕
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) 🖊
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [0279.perfect-squares](./problems/279.perfect-squares.md)
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 🆕
- [0322.coin-change](./problems/322.coin-change.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0365.water-and-jug-problem](./problems/365.water-and-jug-problem.md) 🆕
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md) 🆕
- [0494.target-sum](./problems/494.target-sum.md) 🆕
- [0494.target-sum](./problems/494.target-sum.md)
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
- [0877.stone-game](./problems/877.stone-game.md)
- [0887.super-egg-drop](./problems/887.super-egg-drop.md)
Expand Down Expand Up @@ -204,16 +206,6 @@ Latest updated flashcards (only lists the front page):

### Future Plans

- [0494.target-sum](./todo/494.target-sum.js)

- [0609.find-duplicate-file-in-system](./todo/609.find-duplicate-file-in-system.js)

- [0010.regular-expression-matching](./todo/10.regular-expression-matching.js)

- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./todo/309.best-time-to-buy-and-sell-stock-with-cooldown.js)

- [0365.water-and-jug-problem](./todo/365.water-and-jug-problem.js)

- [Complete Anki Flashcards](./assets/anki/)

- [Collection of String Problem](./todo/str/)
Expand Down
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md) 🆕
- [0015.3-sum](./problems/15.3-sum.md)
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
- [0039.combination-sum](./problems/39.combination-sum.md)
Expand All @@ -126,7 +126,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0055.jump-game](./problems/55.jump-game.md) 🆕
- [0062.unique-paths](./problems/62.unique-paths.md )🆕
- [0075.sort-colors](./problems/75.sort-colors.md)
- [0078.subsets](./problems/78.subsets.md) 🆕
- [0078.subsets](./problems/78.subsets.md)
- [0086.partition-list](./problems/86.partition-list.md)
- [0090.subsets-ii](./problems/90.subsets-ii.md)
- [0091.decode-ways](./problems/91.decode-ways.md) 🆕
Expand All @@ -140,21 +140,23 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md) 🆕
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md) 🆕
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md) 🖊
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)🆕
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md) 🆕
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [0279.perfect-squares](./problems/279.perfect-squares.md)
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md) 🆕
- [0322.coin-change](./problems/322.coin-change.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0365.water-and-jug-problem](./problems/365.water-and-jug-problem.md) 🆕
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md) 🆕
- [0494.target-sum](./problems/494.target-sum.md) 🆕
- [0494.target-sum](./problems/494.target-sum.md)
- [0518.coin-change-2](./problems/518.coin-change-2.md)
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md) 🆕
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
- [0877.stone-game](./problems/877.stone-game.md)
- [0887.super-egg-drop](./problems/887.super-egg-drop.md)
Expand Down Expand Up @@ -200,16 +202,6 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后

### 计划

- [0494.target-sum](./todo/494.target-sum.js)

- [0609.find-duplicate-file-in-system](./todo/609.find-duplicate-file-in-system.js)

- [0010.regular-expression-matching](./todo/10.regular-expression-matching.js)

- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./todo/309.best-time-to-buy-and-sell-stock-with-cooldown.js)

- [0365.water-and-jug-problem](./todo/365.water-and-jug-problem.js)

- [anki 卡片 完善](./assets/anki/)

- [字符串类问题汇总](./todo/str/)
Expand Down
2 changes: 2 additions & 0 deletions backlog/326.power-of-three.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @return {boolean}
*/
var isPowerOfThree = function(n) {
// tag: 数论
// let i = 0;
// while(Math.pow(3, i) < n) {
// i++;
Expand All @@ -55,3 +56,4 @@ var isPowerOfThree = function(n) {
// 巧用整除
return n > 0 && Math.pow(3, 19) % n === 0;
};
// 扩展: 这个方法可以扩展到任意质数,合数则不行
123 changes: 123 additions & 0 deletions problems/365.water-and-jug-problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

## 题目地址
https://leetcode.com/problems/water-and-jug-problem/description/

## 题目描述

```
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs.
If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end.
Operations allowed:
Fill any of the jugs completely with water.
Empty any of the jugs.
Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.
Example 1: (From the famous "Die Hard" example)
Input: x = 3, y = 5, z = 4
Output: True
Example 2:
Input: x = 2, y = 6, z = 5
Output: False
```

## 思路

这是一道关于`数论`的题目,确切地说是关于`裴蜀定理`(英语:Bézout's identity)的题目。

摘自wiki的定义:

```
对任意两个整数 a、b,设 d是它们的最大公约数。那么关于未知数 x和 y的线性丢番图方程(称为裴蜀等式):
ax+by=m
有整数解 (x,y) 当且仅当 m是 d的整数倍。裴蜀等式有解时必然有无穷多个解。
```

因此这道题可以完全转化为`裴蜀定理`

## 关键点解析

- 数论
- 裴蜀定理

## 代码
```js


/*
* @lc app=leetcode id=365 lang=javascript
*
* [365] Water and Jug Problem
*
* https://leetcode.com/problems/water-and-jug-problem/description/
*
* algorithms
* Medium (28.76%)
* Total Accepted: 27K
* Total Submissions: 93.7K
* Testcase Example: '3\n5\n4'
*
* You are given two jugs with capacities x and y litres. There is an infinite
* amount of water supply available. You need to determine whether it is
* possible to measure exactly z litres using these two jugs.
*
* If z liters of water is measurable, you must have z liters of water
* contained within one or both buckets by the end.
*
* Operations allowed:
*
*
* Fill any of the jugs completely with water.
* Empty any of the jugs.
* Pour water from one jug into another till the other jug is completely full
* or the first jug itself is empty.
*
*
* Example 1: (From the famous "Die Hard" example)
*
*
* Input: x = 3, y = 5, z = 4
* Output: True
*
*
* Example 2:
*
*
* Input: x = 2, y = 6, z = 5
* Output: False
*
*/
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @return {boolean}
*/
var canMeasureWater = function(x, y, z) {
if (x + y < z) return false;

if (z === 0) return true;

if (x === 0) return y === z;

if (y === 0) return x === z;

function GCD(a, b) {
let min = Math.min(a, b);
while (min) {
if (a % min === 0 && b % min === 0) return min;
min--;
}
return 1;
}

return z % GCD(x, y) === 0;
};
```
6 changes: 5 additions & 1 deletion problems/494.target-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ target的总数一共有多少种,这是一道我们之前做过的题目,
## 关键点解析

- 对元素进行分组,分组的依据是符号, 是`+` 或者 `-`
- 通过数学公式推导可以简化我们的求解过程,这需要一点`数学知识和数学意志`
- 通过数学公式推导可以简化我们的求解过程,这需要一点`数学知识和数学意识`

## 代码
```js
Expand Down Expand Up @@ -132,3 +132,7 @@ var findTargetSumWays = function(nums, S) {
return sumCount(nums, (S + sum) >> 1);
};
```

## 扩展

如果这道题目并没有限定所有的元素以及target都是正数怎么办?
Loading

0 comments on commit c25465e

Please sign in to comment.