From ee3e483505af9c53fb1e374bd0de9647f6e9b729 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:19:52 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E8=A1=A5=E5=85=85=200714=20=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=20JavaScript=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=20=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...买卖股票的最佳时机含手续费.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index c6a147b4..576f5f85 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -262,6 +262,34 @@ var maxProfit = function(prices, fee) { } return result }; + +// 动态规划 +/** + * @param {number[]} prices + * @param {number} fee + * @return {number} + */ +var maxProfit = function(prices, fee) { + // 滚动数组 + // have表示当天持有股票的最大收益 + // notHave表示当天不持有股票的最大收益 + // 把手续费算在买入价格中 + let n = prices.length, + have = -prices[0]-fee, // 第0天持有股票的最大收益 + notHave = 0; // 第0天不持有股票的最大收益 + for (let i = 1; i < n; i++) { + // 第i天持有股票的最大收益由两种情况组成 + // 1、第i-1天就已经持有股票,第i天什么也没做 + // 2、第i-1天不持有股票,第i天刚买入 + have = Math.max(have, notHave - prices[i] - fee); + // 第i天不持有股票的最大收益由两种情况组成 + // 1、第i-1天就已经不持有股票,第i天什么也没做 + // 2、第i-1天持有股票,第i天刚卖出 + notHave = Math.max(notHave, have + prices[i]); + } + // 最后手中不持有股票,收益才能最大化 + return notHave; +}; ``` From 35d30c93dd7c18893c77f96b9c1daa6b58649ac3 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sun, 3 Oct 2021 10:33:51 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200062=20=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84=20JavaScript=E7=89=88=E6=9C=AC=20?= =?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 我认为将dp数组的值全部初始化为1是可以的,因为由状态转移方程:dp[i][j] = dp[i-1][j] + dp[i][j-1] 可知,当前dp[i][j]的值与其自身的原始值并无联系。所有将dp的所有值都初始化为1,可以减少代码量,同时也不会影响代码的整体思路。 --- problems/0062.不同路径.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index af3a8f40..31896fd1 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -327,6 +327,25 @@ var uniquePaths = function(m, n) { return dp[m - 1][n - 1] }; ``` +>版本二:直接将dp数值值初始化为1 +```javascript +/** + * @param {number} m + * @param {number} n + * @return {number} + */ +var uniquePaths = function(m, n) { + let dp = new Array(m).fill(1).map(() => new Array(n).fill(1)); + // dp[i][j] 表示到达(i,j) 点的路径数 + for (let i=1; i Date: Sun, 3 Oct 2021 18:59:22 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md=20=E6=B7=BB=E5=8A=A0=E9=80=92?= =?UTF-8?q?=E5=BD=92=E8=A7=A3=E6=B3=95=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index ef2664eb..0a8b3622 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -96,6 +96,26 @@ public: }; ``` +我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 + +具体代码如下(带详细注释): + +```c++ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + // 如果链表只有一个节点,返回自身 + if (head->next == NULL) return head; + // 递归调用,翻转第二个节点开始往后的链表 + ListNode *last = reverseList(head->next); + // 翻转头节点与第二个节点的指向 + head->next->next = head; + // 此时的 head 节点为尾节点,next 需要指向 NULL + head->next = NULL; + return last; + } +}; +``` ## 其他语言版本 From 8b6346c4e8d02a68f953a9f350376d68ac9d42eb Mon Sep 17 00:00:00 2001 From: Evan Yang Date: Sun, 3 Oct 2021 19:07:45 +0800 Subject: [PATCH 04/16] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md=20=E4=BF=AE=E5=A4=8DJava?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E9=80=92=E5=BD=92=E5=86=99=E6=B3=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 0a8b3622..4e450a1b 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -155,9 +155,9 @@ class Solution { temp = cur.next;// 先保存下一个节点 cur.next = prev;// 反转 // 更新prev、cur位置 - prev = cur; - cur = temp; - return reverse(prev, cur); + // prev = cur; + // cur = temp; + return reverse(cur, temp); } } ``` From 0ff89f039dd1b6444e4f0ae0d3fd0477ab271812 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Sun, 3 Oct 2021 19:19:31 +0800 Subject: [PATCH 05/16] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 纠正笔误深度和高度 --- problems/0110.平衡二叉树.md | 100 ++++++++++++++++++------------- 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 7858fa60..41cf1ef7 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -125,9 +125,10 @@ public: 1. 明确递归函数的参数和返回值 -参数的话为传入的节点指针,就没有其他参数需要传递了,返回值要返回传入节点为根节点树的深度。 +参数:当前传入节点。 +返回值:以当前传入节点为根节点的树的高度。 -那么如何标记左右子树是否差值大于1呢。 +那么如何标记左右子树是否差值大于1呢? 如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。 @@ -136,9 +137,9 @@ public: 代码如下: -``` +```CPP // -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度 -int getDepth(TreeNode* node) +int getHeight(TreeNode* node) ``` 2. 明确终止条件 @@ -147,7 +148,7 @@ int getDepth(TreeNode* node) 代码如下: -``` +```CPP if (node == NULL) { return 0; } @@ -155,23 +156,23 @@ if (node == NULL) { 3. 明确单层递归的逻辑 -如何判断当前传入节点为根节点的二叉树是否是平衡二叉树呢,当然是左子树高度和右子树高度相差。 +如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值。 -分别求出左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉树了。 +分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了。 代码如下: ```CPP -int leftDepth = depth(node->left); // 左 -if (leftDepth == -1) return -1; -int rightDepth = depth(node->right); // 右 -if (rightDepth == -1) return -1; +int leftHeight = getHeight(node->left); // 左 +if (leftHeight == -1) return -1; +int rightHeight = getHeight(node->right); // 右 +if (rightHeight == -1) return -1; int result; -if (abs(leftDepth - rightDepth) > 1) { // 中 +if (abs(leftHeight - rightHeight) > 1) { // 中 result = -1; } else { - result = 1 + max(leftDepth, rightDepth); // 以当前节点为根节点的最大高度 + result = 1 + max(leftHeight, rightHeight); // 以当前节点为根节点的树的最大高度 } return result; @@ -180,27 +181,27 @@ return result; 代码精简之后如下: ```CPP -int leftDepth = getDepth(node->left); -if (leftDepth == -1) return -1; -int rightDepth = getDepth(node->right); -if (rightDepth == -1) return -1; -return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth); +int leftHeight = getHeight(node->left); +if (leftHeight == -1) return -1; +int rightHeight = getHeight(node->right); +if (rightHeight == -1) return -1; +return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight); ``` 此时递归的函数就已经写出来了,这个递归的函数传入节点指针,返回以该节点为根节点的二叉树的高度,如果不是二叉平衡树,则返回-1。 -getDepth整体代码如下: +getHeight整体代码如下: ```CPP -int getDepth(TreeNode* node) { +int getHeight(TreeNode* node) { if (node == NULL) { return 0; } - int leftDepth = getDepth(node->left); - if (leftDepth == -1) return -1; - int rightDepth = getDepth(node->right); - if (rightDepth == -1) return -1; - return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth); + int leftHeight = getHeight(node->left); + if (leftHeight == -1) return -1; + int rightHeight = getHeight(node->right); + if (rightHeight == -1) return -1; + return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight); } ``` @@ -210,18 +211,18 @@ int getDepth(TreeNode* node) { class Solution { public: // 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1 - int getDepth(TreeNode* node) { + int getHeight(TreeNode* node) { if (node == NULL) { return 0; } - int leftDepth = getDepth(node->left); - if (leftDepth == -1) return -1; // 说明左子树已经不是二叉平衡树 - int rightDepth = getDepth(node->right); - if (rightDepth == -1) return -1; // 说明右子树已经不是二叉平衡树 - return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth); + int leftHeight = getHeight(node->left); + if (leftHeight == -1) return -1; + int rightHeight = getHeight(node->right); + if (rightHeight == -1) return -1; + return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight); } bool isBalanced(TreeNode* root) { - return getDepth(root) == -1 ? false : true; + return getHeight(root) == -1 ? false : true; } }; ``` @@ -498,20 +499,35 @@ class Solution { ## Python 递归法: -```python +```python3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: def isBalanced(self, root: TreeNode) -> bool: - return True if self.getDepth(root) != -1 else False + if self.get_height(root) != -1: + return True + else: + return False - #返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1 - def getDepth(self, node): - if not node: + def get_height(self, root: TreeNode) -> int: + # Base Case + if not root: return 0 - leftDepth = self.getDepth(node.left) - if leftDepth == -1: return -1 #说明左子树已经不是二叉平衡树 - rightDepth = self.getDepth(node.right) - if rightDepth == -1: return -1 #说明右子树已经不是二叉平衡树 - return -1 if abs(leftDepth - rightDepth)>1 else 1 + max(leftDepth, rightDepth) + # 左 + if (left_height := self.get_height(root.left)) == -1: + return -1 + # 右 + if (right_height := self.get_height(root.right)) == -1: + return -1 + # 中 + if abs(left_height - right_height) > 1: + return -1 + else: + return 1 + max(left_height, right_height) ``` 迭代法: From dab89905adaac01f541ecb71f2a9e30980c88249 Mon Sep 17 00:00:00 2001 From: Evan Yang Date: Sun, 3 Oct 2021 19:21:53 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md=20=E6=B7=BB=E5=8A=A0=E9=80=92?= =?UTF-8?q?=E5=BD=92=E8=A7=A3=E6=B3=95=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 4e450a1b..0c35f7d7 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -104,8 +104,10 @@ public: class Solution { public: ListNode* reverseList(ListNode* head) { - // 如果链表只有一个节点,返回自身 + // 边缘条件判断 + if(head == NULL) return NULL; if (head->next == NULL) return head; + // 递归调用,翻转第二个节点开始往后的链表 ListNode *last = reverseList(head->next); // 翻转头节点与第二个节点的指向 From f1aa928e1cc8349a26b3ceb37184a6216f272736 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Sun, 3 Oct 2021 20:09:23 +0800 Subject: [PATCH 07/16] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修订python3语法规范lint --- problems/0257.二叉树的所有路径.md | 43 ++++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index c85186d5..f902aab2 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -404,33 +404,41 @@ class Solution { } } ``` - -Python: -```Python +--- +Python: +递归法+隐形回溯 +```Python3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - """二叉树的所有路径 递归法""" - def binaryTreePaths(self, root: TreeNode) -> List[str]: - path, result = '', [] + path = '' + result = [] + if not root: return result self.traversal(root, path, result) return result - def traversal(self, cur: TreeNode, path: List, result: List): + def traversal(self, cur: TreeNode, path: str, result: List[str]) -> None: path += str(cur.val) - # 如果当前节点为叶子节点,添加路径到结果中 - if not (cur.left or cur.right): + # 若当前节点为leave,直接输出 + if not cur.left and not cur.right: result.append(path) - return - + if cur.left: + # + '->' 是隐藏回溯 self.traversal(cur.left, path + '->', result) - + if cur.right: self.traversal(cur.right, path + '->', result) - ``` - -```python + +迭代法: + +```python3 from collections import deque @@ -457,7 +465,8 @@ class Solution: return result ``` - + +--- Go: ```go @@ -482,7 +491,7 @@ func binaryTreePaths(root *TreeNode) []string { return res } ``` - +--- JavaScript: 1.递归版本 From 5570ce5aafad0583fab8dc5e0b77c824870a557b Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:05:35 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=E8=A7=A3=E5=86=B3=200416=20=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86=20Go=20=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index fd20f68a..05c272c6 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -227,7 +227,7 @@ class Solution: ``` Go: -``` +```go func canPartition(nums []int) bool { /** 动态五部曲: From af819a97267fdf154dea9367425efddfce793757 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:21:46 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=E5=88=A0=E9=99=A4=200494=20=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C=20JavaScript=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E7=9A=84=E6=8E=92=E5=BA=8F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对原数组升序排序是没有必要的 --- problems/0494.目标和.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 210ac749..00771c22 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -371,7 +371,6 @@ const findTargetSumWays = (nums, target) => { } const halfSum = (target + sum) / 2; - nums.sort((a, b) => a - b); let dp = new Array(halfSum+1).fill(0); dp[0] = 1; From 5ecfb3aaca49e37520f53249028a8fcf324a1a7f Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:43:48 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=E8=A7=A3=E5=86=B3=200070=20=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=20c++=E4=BB=A3=E7=A0=81=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 104b2d5a..3851e7a5 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -88,7 +88,7 @@ 以上分析完毕,C++代码如下: -``` +```c++ class Solution { public: int climbStairs(int n) { From 78efc0ab870f97f015a7babefad86092624889aa Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 4 Oct 2021 09:55:01 +0800 Subject: [PATCH 11/16] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200279=20=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E5=B9=B3=E6=96=B9=E6=95=B0=20JavaScript=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 首先 0 不是题目中所说的完全平方数,所有 i 的范围应该从 1 开始; 其次,i <= n 不太合理,增加了大量无用计算,应改成 i**2 (即 i^2)<= n 更为合适 --- problems/0279.完全平方数.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index a00ed47e..b1af7e95 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -334,8 +334,8 @@ var numSquares1 = function(n) { let dp = new Array(n + 1).fill(Infinity) dp[0] = 0 - for(let i = 0; i <= n; i++) { - let val = i * i + for(let i = 1; i**2 <= n; i++) { + let val = i**2 for(let j = val; j <= n; j++) { dp[j] = Math.min(dp[j], dp[j - val] + 1) } From 7b6fd761297d0d90c80180e8dcce17feff9c3483 Mon Sep 17 00:00:00 2001 From: Asterisk <44215173+casnz1601@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:25:43 +0800 Subject: [PATCH 12/16] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 进一步探讨左右子树情况 --- problems/二叉树中递归带着回溯.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 71a3ee5c..0a386fe1 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -145,22 +145,22 @@ if (cur->right) { } ``` -此时就没有回溯了,这个代码就是通过不了的了。 +因为在递归右子树之前需要还原path,所以在左子树递归后必须为了右子树而进行回溯操作。而只右子树自己不添加回溯也可以成功AC。 -如果想把回溯加上,就要 在上面代码的基础上,加上回溯,就可以AC了。 +因此,在上面代码的基础上,再加上左右子树的回溯代码,就可以AC了。 ```CPP if (cur->left) { path += "->"; traversal(cur->left, path, result); // 左 - path.pop_back(); // 回溯 - path.pop_back(); + path.pop_back(); // 回溯,抛掉val + path.pop_back(); // 回溯,抛掉-> } if (cur->right) { path += "->"; traversal(cur->right, path, result); // 右 - path.pop_back(); // 回溯 - path.pop_back(); + path.pop_back(); // 回溯(非必要) + path.pop_back(); } ``` From abd3358314f9580a5a43540c22aa861cb20f0105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Tue, 5 Oct 2021 14:21:08 +0800 Subject: [PATCH 13/16] =?UTF-8?q?Update=200206.=E7=BF=BB=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 0c35f7d7..ec6f3dca 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -100,7 +100,7 @@ public: 具体代码如下(带详细注释): -```c++ +```CPP class Solution { public: ListNode* reverseList(ListNode* head) { From 7f051a4dfb160d4aa77e5aaf0768fc05cd7a95aa Mon Sep 17 00:00:00 2001 From: DoubleYellowIce <65336599+DoubleYellowIce@users.noreply.github.com> Date: Tue, 5 Oct 2021 15:29:06 +0800 Subject: [PATCH 14/16] =?UTF-8?q?Update=200738.=E5=8D=95=E8=B0=83=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java原先版本中创建了String数组,多次使用Integer.parseInt了方法,这导致不管是耗时还是空间占用都非常高,用时12ms,下面提供一个版本在char数组上原地修改,用时1ms的版本 --- problems/0738.单调递增的数字.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 0db0db15..61175521 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -127,6 +127,7 @@ public: Java: ```java +版本1 class Solution { public int monotoneIncreasingDigits(int N) { String[] strings = (N + "").split(""); @@ -144,6 +145,31 @@ class Solution { } } ``` +java版本1中创建了String数组,多次使用Integer.parseInt了方法,这导致不管是耗时还是空间占用都非常高,用时12ms,下面提供一个版本在char数组上原地修改,用时1ms的版本 +```java +版本2 +class Solution { + public int monotoneIncreasingDigits(int n) { + if (n==0)return 0; + char[] chars= Integer.toString(n).toCharArray(); + int start=Integer.MAX_VALUE;//start初始值设为最大值,这是为了防止当数字本身是单调递增时,没有一位数字需要改成9的情况 + for (int i=chars.length-1;i>0;i--){ + if (chars[i]=start){ + res.append('9'); + }else res.append(chars[i]); + } + return Integer.parseInt(res.toString()); + } +} +``` Python: From c97ef2dad03d83f1711111d644dafbda3a548602 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Wed, 6 Oct 2021 09:58:52 +0800 Subject: [PATCH 15/16] =?UTF-8?q?=E8=A7=A3=E5=86=B30392=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97=20Java=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=97=A0=E6=A0=B7=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0392.判断子序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index cda0c82d..1a8e55fa 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -141,7 +141,7 @@ public: Java: -``` +```java class Solution { public boolean isSubsequence(String s, String t) { int length1 = s.length(); int length2 = t.length(); From 8a35685b3f030672b1cd71d2ad9f357a1d60093a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Wed, 6 Oct 2021 16:19:22 +0800 Subject: [PATCH 16/16] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 3851e7a5..02c995c3 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -88,7 +88,7 @@ 以上分析完毕,C++代码如下: -```c++ +```CPP class Solution { public: int climbStairs(int n) {