diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 01fc7b93..7bb7f746 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -125,7 +125,7 @@ public: } }; ``` -技巧性的东西没有固定的学习方法,还是要多看多练,自己总灵活运用了。 +技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。 ## 其他语言版本 diff --git a/problems/0077.组合.md b/problems/0077.组合.md index e582daf6..4560c5b7 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -456,6 +456,27 @@ const combineHelper = (n, k, startIndex) => { } ``` +## TypeScript + +```typescript +function combine(n: number, k: number): number[][] { + let resArr: number[][] = []; + function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void { + if (tempArr.length === k) { + resArr.push(tempArr.slice()); + return; + } + for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) { + tempArr.push(i); + backTracking(n, k, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(n, k, 1, []); + return resArr; +}; +``` + ## Go diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 81b4304c..94608ec1 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -240,7 +240,29 @@ var combine = function(n, k) { }; ``` +TypeScript: + +```typescript +function combine(n: number, k: number): number[][] { + let resArr: number[][] = []; + function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void { + if (tempArr.length === k) { + resArr.push(tempArr.slice()); + return; + } + for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) { + tempArr.push(i); + backTracking(n, k, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(n, k, 1, []); + return resArr; +}; +``` + C: + ```c int* path; int pathTop; diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 16bd1f2c..d08707b4 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -87,7 +87,7 @@ class Solution { private: vector> result; vector path; - void backtracking(vector& nums, int startIndex, vector& used) { + void backtracking(vector& nums, int startIndex) { result.push_back(path); unordered_set uset; for (int i = startIndex; i < nums.size(); i++) { @@ -96,7 +96,7 @@ private: } uset.insert(nums[i]); path.push_back(nums[i]); - backtracking(nums, i + 1, used); + backtracking(nums, i + 1); path.pop_back(); } } @@ -105,9 +105,8 @@ public: vector> subsetsWithDup(vector& nums) { result.clear(); path.clear(); - vector used(nums.size(), false); sort(nums.begin(), nums.end()); // 去重需要排序 - backtracking(nums, 0, used); + backtracking(nums, 0); return result; } }; diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 5db36687..41463ec1 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -212,7 +212,7 @@ public: }; ``` -如果大家完全理解了本地的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 +如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 # 113. 路径总和ii @@ -496,25 +496,20 @@ class solution: def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]: def traversal(cur_node, remain): - if not cur_node.left and not cur_node.right and remain == 0: - result.append(path[:]) - return - - if not cur_node.left and not cur_node.right: return + if not cur_node.left and not cur_node.right: + if remain == 0: + result.append(path[:]) + return if cur_node.left: path.append(cur_node.left.val) - remain -= cur_node.left.val - traversal(cur_node.left, remain) + traversal(cur_node.left, remain-cur_node.left.val) path.pop() - remain += cur_node.left.val if cur_node.right: path.append(cur_node.right.val) - remain -= cur_node.right.val - traversal(cur_node.right, remain) + traversal(cur_node.right, remain-cur_node.left.val) path.pop() - remain += cur_node.right.val result, path = [], [] if not root: @@ -524,6 +519,30 @@ class solution: return result ``` +**迭代法,用第二个队列保存目前的总和与路径** +```python +class Solution: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + if not root: + return [] + que, temp = deque([root]), deque([(root.val, [root.val])]) + result = [] + while que: + for _ in range(len(que)): + node = que.popleft() + value, path = temp.popleft() + if (not node.left) and (not node.right): + if value == targetSum: + result.append(path) + if node.left: + que.append(node.left) + temp.append((node.left.val+value, path+[node.left.val])) + if node.right: + que.append(node.right) + temp.append((node.right.val+value, path+[node.right.val])) + return result +``` + ## go 112. 路径总和 diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index c45917db..3457c4b3 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -354,6 +354,32 @@ class MyStack: return len(self.queue_in) == 0 ``` +优化,使用一个队列实现 +```python +class MyStack: + + def __init__(self): + self.que = deque() + + def push(self, x: int) -> None: + self.que.append(x) + + def pop(self) -> int: + if self.empty(): + return None + for i in range(len(self.que)-1): + self.que.append(self.que.popleft()) + return self.que.popleft() + + def top(self) -> int: + if self.empty(): + return None + return self.que[-1] + + def empty(self) -> bool: + return not self.que +``` + Go: diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index ce215a7e..f269450f 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -193,7 +193,7 @@ public: # 扩展 -大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本地中的单调队列实现就是固定的写法哈。 +大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本题中的单调队列实现就是固定的写法哈。 大家貌似对deque也有一些疑惑,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过啦),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index 940d81b8..aaf27e61 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -127,7 +127,7 @@ public: ``` -C++测试用例有超过两个树相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 +C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 但java就不用考虑这个限制,java里的int也是四个字节吧,也有可能leetcode后台对不同语言的测试数据不一样。 diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 210b492d..5c86e478 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -32,7 +32,7 @@ ## 思路 -为了了满足更多的小孩,就不要造成饼干尺寸的浪费。 +为了满足更多的小孩,就不要造成饼干尺寸的浪费。 大尺寸的饼干既可以满足胃口大的孩子也可以满足胃口小的孩子,那么就应该优先满足胃口大的。 diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index d9928b8f..913aec65 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -54,7 +54,7 @@ 当s[i]与s[j]相等时,这就复杂一些了,有如下三种情况 * 情况一:下标i 与 j相同,同一个字符例如a,当然是回文子串 -* 情况二:下标i 与 j相差为1,例如aa,也是文子串 +* 情况二:下标i 与 j相差为1,例如aa,也是回文子串 * 情况三:下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看dp[i + 1][j - 1]是否为true。 以上三种情况分析完了,那么递归公式如下: @@ -178,7 +178,7 @@ public: 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 -首先确定回文串,就是找中心然后想两边扩散看是不是对称的就可以了。 +首先确定回文串,就是找中心然后向两边扩散看是不是对称的就可以了。 **在遍历中心点的时候,要注意中心点有两种情况**。 diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index f48ecf4d..ffd5490d 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -157,7 +157,7 @@ class Solution { ```python class Solution: def lemonadeChange(self, bills: List[int]) -> bool: - five, ten, twenty = 0, 0, 0 + five, ten = 0, 0 for bill in bills: if bill == 5: five += 1 @@ -169,10 +169,8 @@ class Solution: if ten > 0 and five > 0: ten -= 1 five -= 1 - twenty += 1 elif five > 2: five -= 3 - twenty += 1 else: return False return True diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index a2444cc1..4b7a978c 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -106,22 +106,15 @@ class Solution { ```python class Solution: def validMountainArray(self, arr: List[int]) -> bool: - if len(arr) < 3 : - return False - - i = 1 - flagIncrease = False # 上升 - flagDecrease = False # 下降 - - while i < len(arr) and arr[i-1] < arr[i]: - flagIncrease = True - i += 1 - - while i < len(arr) and arr[i-1] > arr[i]: - flagDecrease = True - i += 1 - - return i == len(arr) and flagIncrease and flagDecrease + left, right = 0, len(arr)-1 + + while left < len(arr)-1 and arr[left+1] > arr[left]: + left += 1 + + while right > 0 and arr[right-1] > arr[right]: + right -= 1 + + return left == right and right != 0 and left != len(arr)-1 ``` diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md index 44fcbdd5..8c76614f 100644 --- a/problems/栈与队列理论基础.md +++ b/problems/栈与队列理论基础.md @@ -15,7 +15,7 @@ 那么我这里在列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,相信使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 1. C++中stack 是容器么? -2. 我们使用的stack是属于那个版本的STL? +2. 我们使用的stack是属于哪个版本的STL? 3. 我们使用的STL中stack是如何实现的? 4. stack 提供迭代器来遍历stack空间么?