From ef747f7921ccd6b64a6de661847eac88528bbb40 Mon Sep 17 00:00:00 2001 From: Qiyu Liang <61932152+Guicai996@users.noreply.github.com> Date: Sun, 27 Feb 2022 19:57:40 +0800 Subject: [PATCH 01/17] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了L48-54,增选解释了一种特殊情况:节点本身p(q),它拥有一个子孙节点q(p)。 --- ...21\345\205\254\345\205\261\347\245\226\345\205\210.md" | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 6213aeaa2c..5b2abd181b 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -45,9 +45,13 @@ 接下来就看如何判断一个节点是节点q和节点p的公共公共祖先呢。 -**如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。** +**首先最容易想到的一个情况:如果找到一个节点,发现左子树出现结点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。** -使用后序遍历,回溯的过程,就是从低向上遍历节点,一旦发现如何这个条件的节点,就是最近公共节点了。 +**但是很多人容易忽略一个情况,就是节点本身p(q),它拥有一个子孙节点q(p)。** + +使用后序遍历,回溯的过程,就是从低向上遍历节点,一旦发现满足第一种情况的节点,就是最近公共节点了。 + +**但是如果p或者q本身就是最近公共祖先呢?其实只需要找到一个节点是p或者q的时候,直接返回当前节点,无需继续递归子树。如果接下来的遍历中找到了后继节点满足第一种情况则修改返回值为后继节点,否则,继续返回已找到的节点即可。为什么满足第一种情况的节点一定是p或q的后继节点呢?大家可以仔细思考一下。** 递归三部曲: From 32dada4f6d071c10643dd69c356aeba74601cf21 Mon Sep 17 00:00:00 2001 From: Qiyu Liang <61932152+Guicai996@users.noreply.github.com> Date: Mon, 28 Feb 2022 15:01:23 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II=E5=A2=9E=E5=8A=A0=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E4=BC=98=E5=8C=96=E7=89=88=E6=9C=ACc++=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0063.不同路径II,L159-190 增加空间优化版本c++代码 --- ...\345\220\214\350\267\257\345\276\204II.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8e82007e7a..86c42150a5 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -155,6 +155,39 @@ public: * 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度 * 空间复杂度:$O(n × m)$ + +同样我们给出空间优化版本: +```CPP +class Solution { +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + if (obstacleGrid[0][0] == 1) + return 0; + vector dp(obstacleGrid[0].size()); + for (int j = 0; j < dp.size(); ++j) + if (obstacleGrid[0][j] == 1) + dp[j] = 0; + else if (j == 0) + dp[j] = 1; + else + dp[j] = dp[j-1]; + + for (int i = 1; i < obstacleGrid.size(); ++i) + for (int j = 0; j < dp.size(); ++j){ + if (obstacleGrid[i][j] == 1) + dp[j] = 0; + else if (j != 0) + dp[j] = dp[j] + dp[j-1]; + } + return dp.back(); + } +}; +``` + +* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度 +* 空间复杂度:$O(m)$ + + ## 总结 本题是[62.不同路径](https://programmercarl.com/0062.不同路径.html)的障碍版,整体思路大体一致。 From cc302a9e8d43440605c4a61bde398784800c2b5e Mon Sep 17 00:00:00 2001 From: Aaron-Lin-74 <84072071+Aaron-Lin-74@users.noreply.github.com> Date: Fri, 4 Mar 2022 10:52:42 +1100 Subject: [PATCH 03/17] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The variable cur could be null, should add the union to the type of cur. --- ...\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 4e7bdd34a8..c0e0be3e8d 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -324,7 +324,7 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { head = head.next; } if (head === null) return head; - let pre: ListNode = head, cur: ListNode = head.next; + let pre: ListNode = head, cur: ListNode | null = head.next; // 删除非头部节点 while (cur) { if (cur.val === val) { From 09c1bd818abf8da2c1c426a06e46f2063b0089b0 Mon Sep 17 00:00:00 2001 From: Aaron-Lin-74 <84072071+Aaron-Lin-74@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:12:33 +1100 Subject: [PATCH 04/17] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Creating a dummy head variable makes the logic clear. --- ...\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 4e7bdd34a8..d2ba1ae852 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -342,14 +342,14 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { ```typescript function removeElements(head: ListNode | null, val: number): ListNode | null { - head = new ListNode(0, head); - let pre: ListNode = head, cur: ListNode = head.next; + let dummyHead = new ListNode(0, head); + let pre: ListNode = dummyHead, cur: ListNode | null = dummyHead.next; // 删除非头部节点 while (cur) { if (cur.val === val) { pre.next = cur.next; } else { - pre = pre.next; + pre = cur; } cur = cur.next; } From e83846db0413bdcb6be0509eb987d3d2d685de50 Mon Sep 17 00:00:00 2001 From: ClorisMoQi <748709762@qq.com> Date: Thu, 3 Mar 2022 22:40:31 -0800 Subject: [PATCH 05/17] add explanation for go slice --- ...37\346\230\257\345\244\232\345\244\247\357\274\237.md" | 6 ++++++ ...37\347\220\206\350\256\262\350\247\243\357\274\211.md" | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 4de5659724..66db2b833b 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -226,7 +226,13 @@ Python: Go: +Go中slice的`append`操作和C++中vector的扩容机制基本相同。 +说是基本呢,其实是因为大家平时刷题和工作中遇到的数据不会特别大。 + +具体来说,当当前slice的长度小于**1024**时,执行`append`操作,新slice的capacity会变成当前的2倍;而当slice长度大于等于**1024**时,slice的扩容变成了每次增加当前slice长度的**1/4**。 + +在Go Slice的底层实现中,如果capacity不够时,会做一个reslice的操作,底层数组也会重新被复制到另一块内存区域中,所以`append`一个元素,不一定是O(1), 也可能是O(n)哦。 ----------------------- diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 11a72e2db0..28317e6c14 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -171,6 +171,14 @@ Python: Go: +Go中slice的`append`操作和C++中vector的扩容机制基本相同。 + +说是基本呢,其实是因为大家平时刷题和工作中遇到的数据不会特别大。 + +具体来说,当当前slice的长度小于**1024**时,执行`append`操作,新slice的capacity会变成当前的2倍;而当slice长度大于等于**1024**时,slice的扩容变成了每次增加当前slice长度的**1/4**。 + +在Go Slice的底层实现中,如果capacity不够时,会做一个reslice的操作,底层数组也会重新被复制到另一块内存区域中,所以`append`一个元素,不一定是O(1), 也可能是O(n)哦。 + From 3ec2f7ff537407c036edabfab126d9190e0d299b Mon Sep 17 00:00:00 2001 From: ClorisMoQi <748709762@qq.com> Date: Thu, 3 Mar 2022 22:42:07 -0800 Subject: [PATCH 06/17] add explanation for go slice --- ...\237\346\230\257\345\244\232\345\244\247\357\274\237.md" | 6 ------ 1 file changed, 6 deletions(-) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index 66db2b833b..4de5659724 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -226,13 +226,7 @@ Python: Go: -Go中slice的`append`操作和C++中vector的扩容机制基本相同。 -说是基本呢,其实是因为大家平时刷题和工作中遇到的数据不会特别大。 - -具体来说,当当前slice的长度小于**1024**时,执行`append`操作,新slice的capacity会变成当前的2倍;而当slice长度大于等于**1024**时,slice的扩容变成了每次增加当前slice长度的**1/4**。 - -在Go Slice的底层实现中,如果capacity不够时,会做一个reslice的操作,底层数组也会重新被复制到另一块内存区域中,所以`append`一个元素,不一定是O(1), 也可能是O(n)哦。 ----------------------- From 8cf3fabb60a29966bb725b223ada9d92d7aa5bb1 Mon Sep 17 00:00:00 2001 From: Aaron-Lin-74 <84072071+Aaron-Lin-74@users.noreply.github.com> Date: Sat, 5 Mar 2022 11:41:05 +1100 Subject: [PATCH 07/17] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TypeScript version code that better matches your c++ code example. --- ...55\347\232\204\350\212\202\347\202\271.md" | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index bf1fd5e19d..ce75e0d79f 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -254,32 +254,20 @@ TypeScript: ```typescript function swapPairs(head: ListNode | null): ListNode | null { - /** - * 初始状态: - * curNode -> node1 -> node2 -> tmepNode - * 转换过程: - * curNode -> node2 - * curNode -> node2 -> node1 - * curNode -> node2 -> node1 -> tempNode - * curNode = node1 - */ - let retNode: ListNode | null = new ListNode(0, head), - curNode: ListNode | null = retNode, - node1: ListNode | null = null, - node2: ListNode | null = null, - tempNode: ListNode | null = null; - - while (curNode && curNode.next && curNode.next.next) { - node1 = curNode.next; - node2 = curNode.next.next; - tempNode = node2.next; - curNode.next = node2; - node2.next = node1; - node1.next = tempNode; - curNode = node1; - } - return retNode.next; -}; + const dummyHead: ListNode = new ListNode(0, head); + let cur: ListNode = dummyHead; + while(cur.next !== null && cur.next.next !== null) { + const tem: ListNode = cur.next; + const tem1: ListNode = cur.next.next.next; + + cur.next = cur.next.next; // step 1 + cur.next.next = tem; // step 2 + cur.next.next.next = tem1; // step 3 + + cur = cur.next.next; + } + return dummyHead.next; +} ``` Kotlin: From d285d3d6b812a607bc31501a73a50a63222398d3 Mon Sep 17 00:00:00 2001 From: Younglesszzz <61218410+Younglesszzz@users.noreply.github.com> Date: Sun, 6 Mar 2022 20:54:25 +0800 Subject: [PATCH 08/17] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF+=E8=AE=B0=E5=BF=86=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 经过提交验证,其实memo[startIndex] = 1的这个逻辑根本没有用到,因为如果返回true,那么会如同dfs一样直接返回,不会再进行下一步的backtracking搜索,本题的记忆法核心是令memo[startIndex]置为-1,来避免从相同的startIndex开始拆分,导致程序进行大量重复运算,这应该也是本题剪枝方法的核心。 --- ...25\350\257\215\346\213\206\345\210\206.md" | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 1653a81a14..a54b849af0 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -251,30 +251,34 @@ class Solution { // 回溯法+记忆化 class Solution { + private Set set; + private int[] memo; public boolean wordBreak(String s, List wordDict) { - Set wordDictSet = new HashSet(wordDict); - int[] memory = new int[s.length()]; - return backTrack(s, wordDictSet, 0, memory); + memo = new int[s.length()]; + set = new HashSet<>(wordDict); + return backtracking(s, 0); } - - public boolean backTrack(String s, Set wordDictSet, int startIndex, int[] memory) { - // 结束条件 - if (startIndex >= s.length()) { + + public boolean backtracking(String s, int startIndex) { + // System.out.println(startIndex); + if (startIndex == s.length()) { return true; } - if (memory[startIndex] != 0) { - // 此处认为:memory[i] = 1 表示可以拼出i 及以后的字符子串, memory[i] = -1 表示不能 - return memory[startIndex] == 1 ? true : false; + if (memo[startIndex] == -1) { + return false; } - for (int i = startIndex; i < s.length(); ++i) { - // 处理 递归 回溯 循环不变量:[startIndex, i + 1) - String word = s.substring(startIndex, i + 1); - if (wordDictSet.contains(word) && backTrack(s, wordDictSet, i + 1, memory)) { - memory[startIndex] = 1; - return true; + + for (int i = startIndex; i < s.length(); i++) { + String sub = s.substring(startIndex, i + 1); + // 拆分出来的单词无法匹配 + if (!set.contains(sub)) { + continue; } + boolean res = backtracking(s, i + 1); + if (res) return true; } - memory[startIndex] = -1; + // 这里是关键,找遍了startIndex~s.length()也没能完全匹配,标记从startIndex开始不能找到 + memo[startIndex] = -1; return false; } } From 94bf8916eef21cd40b2eb7b04cf67b2bf32ed6f6 Mon Sep 17 00:00:00 2001 From: Aaron-Lin-74 <84072071+Aaron-Lin-74@users.noreply.github.com> Date: Mon, 7 Mar 2022 10:15:06 +1100 Subject: [PATCH 09/17] =?UTF-8?q?Update=201002.=E6=9F=A5=E6=89=BE=E5=B8=B8?= =?UTF-8?q?=E7=94=A8=E5=AD=97=E7=AC=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add TypeScript solution. --- ...70\347\224\250\345\255\227\347\254\246.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index 7c5566d333..efad1e6a95 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -253,6 +253,41 @@ var commonChars = function (words) { return res }; ``` + +TypeScript +```ts +function commonChars(words: string[]): string[] { + let result: string[] = []; + if (words.length === 0) return result; + const size: number = 26; + const hash: number[] = new Array(size).fill(0); + const otherHash: number[] = new Array(size).fill(0); + let pivot: number = 'a'.charCodeAt(0); + // First word + for (let character of words[0]) { + hash[character.charCodeAt(0) - pivot]++; + } + // Other words + for (let i = 1; i < words.length; i++) { + for (let character of words[i]) { + otherHash[character.charCodeAt(0) - pivot]++; + } + // Update the first hash with min + for (let j = 0; j < size; j++) { + hash[j] = Math.min(hash[j], otherHash[j]); + } + // Reset otherHash + otherHash.fill(0); + } + // Construct the result + hash.forEach((element, index) => { + while (element-- > 0) { + result.push(String.fromCharCode(index + pivot)); + } + }); + return result; +} +``` GO ```golang func commonChars(words []string) []string { From b79f3e0fb75ceefdc7b7db1b9f60fd53e3c3fee0 Mon Sep 17 00:00:00 2001 From: wutianjue Date: Mon, 7 Mar 2022 14:38:03 +0800 Subject: [PATCH 10/17] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=AF=8F=E4=B8=AA?= =?UTF-8?q?=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80=E5=A4=A7=E5=80=BC?= =?UTF-8?q?Java=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 7448584854..42380b1533 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1300,23 +1300,23 @@ java代码: ```java class Solution { public List largestValues(TreeNode root) { - List retVal = new ArrayList(); - Queue tmpQueue = new LinkedList(); - if (root != null) tmpQueue.add(root); - - while (tmpQueue.size() != 0){ - int size = tmpQueue.size(); - List lvlVals = new ArrayList(); - for (int index = 0; index < size; index++){ - TreeNode node = tmpQueue.poll(); - lvlVals.add(node.val); - if (node.left != null) tmpQueue.add(node.left); - if (node.right != null) tmpQueue.add(node.right); - } - retVal.add(Collections.max(lvlVals)); - } - - return retVal; + if(root == null){ + return Collections.emptyList(); + } + List result = new ArrayList(); + Queue queue = new LinkedList(); + queue.offer(root); + while(!queue.isEmpty()){ + int max = Integer.MIN_VALUE; + for(int i = queue.size(); i > 0; i--){ + TreeNode node = queue.poll(); + max = Math.max(max, node.val); + if(node.left != null) queue.offer(node.left); + if(node.right != null) queue.offer(node.right); + } + result.add(max); + } + return result; } } ``` From a33f315175ff76061cbafc2b53a309f75db9890f Mon Sep 17 00:00:00 2001 From: tlylt Date: Mon, 7 Mar 2022 15:26:18 +0800 Subject: [PATCH 11/17] Fix spelling error --- ...347\255\211\345\222\214\345\255\220\351\233\206.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 9da1f8d5d3..a4e780ab1b 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -253,14 +253,14 @@ Python: ```python class Solution: def canPartition(self, nums: List[int]) -> bool: - taraget = sum(nums) - if taraget % 2 == 1: return False - taraget //= 2 + target = sum(nums) + if target % 2 == 1: return False + target //= 2 dp = [0] * 10001 for i in range(len(nums)): - for j in range(taraget, nums[i] - 1, -1): + for j in range(target, nums[i] - 1, -1): dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) - return taraget == dp[taraget] + return target == dp[target] ``` Go: ```go From ea31f6e70c277b527f90fce96dacb59a660f8956 Mon Sep 17 00:00:00 2001 From: berserk-112 <40333359+berserk-112@users.noreply.github.com> Date: Wed, 9 Mar 2022 12:05:30 +0800 Subject: [PATCH 12/17] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB=E5=86=B7?= =?UTF-8?q?=E5=86=BB=E6=9C=9F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了java版本的另一种解题思路,无需考虑多种状态,还是只考虑持有与未持有两种状态 --- ...53\345\206\267\345\206\273\346\234\237.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 2dc1e87486..53caa46e7a 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -205,6 +205,29 @@ class Solution { } } ``` +```java +//另一种解题思路 +class Solution { + public int maxProfit(int[] prices) { + int[][] dp = new int[prices.length + 1][2]; + dp[1][0] = -prices[0]; + + for (int i = 2; i <= prices.length; i++) { + /* + dp[i][0] 第i天未持有股票收益; + dp[i][1] 第i天持有股票收益; + 情况一:第i天是冷静期,不能以dp[i-1][1]购买股票,所以以dp[i - 2][1]买股票,没问题 + 情况二:第i天不是冷静期,理论上应该以dp[i-1][1]购买股票,但是第i天不是冷静期说明,第i-1天没有卖出股票, + 则dp[i-1][1]=dp[i-2][1],所以可以用dp[i-2][1]买股票,没问题 + */ + dp[i][0] = Math.max(dp[i - 1][0], dp[i - 2][1] - prices[i - 1]); + dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i - 1]); + } + + return dp[prices.length][1]; + } +} +``` Python: From 6c3c8c7bff1bf90f4a79259c0b7abd2434fd9d72 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 11 Mar 2022 11:35:14 +0800 Subject: [PATCH 13/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 5e9fbdfe7e..df6a395444 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -280,7 +280,7 @@ class Solution: # 返回更新后的以当前root为根节点的新树 return roo -``` +``` **递归法** - 无返回值 ```python @@ -308,7 +308,7 @@ class Solution: return __traverse(root, val) return root -``` +``` **递归法** - 无返回值 - another easier way ```python @@ -378,7 +378,7 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { } return root } -``` +``` 迭代法 @@ -520,5 +520,71 @@ var insertIntoBST = function (root, val) { }; ``` +## TypeScript + +> 递归-有返回值 + +```typescript +function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { + if (root === null) return new TreeNode(val); + if (root.val > val) { + root.left = insertIntoBST(root.left, val); + } else { + root.right = insertIntoBST(root.right, val); + } + return root; +}; +``` + +> 递归-无返回值 + +```typescript +function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { + if (root === null) return new TreeNode(val); + function recur(root: TreeNode | null, val: number) { + if (root === null) { + if (parentNode.val > val) { + parentNode.left = new TreeNode(val); + } else { + parentNode.right = new TreeNode(val); + } + return; + } + parentNode = root; + if (root.val > val) recur(root.left, val); + if (root.val < val) recur(root.right, val); + } + let parentNode: TreeNode = root; + recur(root, val); + return root; +}; +``` + +> 迭代法 + +```typescript +function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { + if (root === null) return new TreeNode(val); + let curNode: TreeNode | null = root; + let parentNode: TreeNode = root; + while (curNode !== null) { + parentNode = curNode; + if (curNode.val > val) { + curNode = curNode.left + } else { + curNode = curNode.right; + } + } + if (parentNode.val > val) { + parentNode.left = new TreeNode(val); + } else { + parentNode.right = new TreeNode(val); + } + return root; +}; +``` + + + -----------------------
From 301dd3e13984a2d6de4711bc08417aa3bd2a1e8d Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 12 Mar 2022 00:15:06 +0800 Subject: [PATCH 14/17] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 2e33353093..cc29bea4c6 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -518,6 +518,70 @@ var deleteNode = function (root, key) { } ``` +## TypeScript + +> 递归法: + +```typescript +function deleteNode(root: TreeNode | null, key: number): TreeNode | null { + if (root === null) return null; + if (root.val === key) { + if (root.left === null && root.right === null) return null; + if (root.left === null) return root.right; + if (root.right === null) return root.left; + let curNode: TreeNode = root.right; + while (curNode.left !== null) { + curNode = curNode.left; + } + curNode.left = root.left; + return root.right; + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; +}; +``` + +> 迭代法: + +```typescript +function deleteNode(root: TreeNode | null, key: number): TreeNode | null { + function removeTargetNode(root: TreeNode): TreeNode | null { + if (root.left === null && root.right === null) return null; + if (root.right === null) return root.left; + if (root.left === null) return root.right; + let curNode: TreeNode | null = root.right; + while (curNode.left !== null) { + curNode = curNode.left; + } + curNode.left = root.left; + return root.right; + } + let preNode: TreeNode | null = null, + curNode: TreeNode | null = root; + while (curNode !== null) { + if (curNode.val === key) break; + preNode = curNode; + if (curNode.val > key) { + curNode = curNode.left; + } else { + curNode = curNode.right; + } + } + if (curNode === null) return root; + if (preNode === null) { + // 删除头节点 + return removeTargetNode(curNode); + } + if (preNode.val > key) { + preNode.left = removeTargetNode(curNode); + } else { + preNode.right = removeTargetNode(curNode); + } + return root; +}; +``` + ----------------------- From cbe5403e45e009f5d906fa168ff5aee086043e57 Mon Sep 17 00:00:00 2001 From: berserk-112 <40333359+berserk-112@users.noreply.github.com> Date: Thu, 24 Mar 2022 11:23:36 +0800 Subject: [PATCH 15/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bf7fcdf43..5dfc04fd8e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ - +贪心:[贪心算法:分发糖果](./problems/0135.分发糖果.md) [贪心算法:根据身高重建队列](./problems/0406.根据身高重建队列.md) 👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定) 👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master) From 5c403d9e589203d8ee7c2beff3c863accb3bce94 Mon Sep 17 00:00:00 2001 From: berserk-112 <40333359+berserk-112@users.noreply.github.com> Date: Fri, 25 Mar 2022 11:08:45 +0800 Subject: [PATCH 16/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dfc04fd8e..a0f4863e0f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -贪心:[贪心算法:分发糖果](./problems/0135.分发糖果.md) [贪心算法:根据身高重建队列](./problems/0406.根据身高重建队列.md) +贪心:[贪心算法:分发糖果](./problems/0135.分发糖果.md) [贪心算法:根据身高重建队列](./problems/0406.根据身高重建队列.md) [贪心算法:我要监控二叉树!](./problems/0968.监控二叉树.md) 👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定) 👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master) From 6d1746db85853e3714b194c659335a71a69d2d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Mon, 28 Mar 2022 09:55:29 +0800 Subject: [PATCH 17/17] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a0f4863e0f..78813ac3a0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -贪心:[贪心算法:分发糖果](./problems/0135.分发糖果.md) [贪心算法:根据身高重建队列](./problems/0406.根据身高重建队列.md) [贪心算法:我要监控二叉树!](./problems/0968.监控二叉树.md) 👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定) 👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)