From b79f3e0fb75ceefdc7b7db1b9f60fd53e3c3fee0 Mon Sep 17 00:00:00 2001 From: wutianjue Date: Mon, 7 Mar 2022 14:38:03 +0800 Subject: [PATCH 1/6] =?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 --- problems/0102.二叉树的层序遍历.md | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 74485848..42380b15 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.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 2/6] Fix spelling error --- problems/0416.分割等和子集.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 9da1f8d5..a4e780ab 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.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 3/6] =?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版本的另一种解题思路,无需考虑多种状态,还是只考虑持有与未持有两种状态 --- ...09.最佳买卖股票时机含冷冻期.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 2dc1e874..53caa46e 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.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 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 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bf7fcdf..5dfc04fd 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 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5dfc04fd..a0f4863e 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 6/6] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a0f4863e..78813ac3 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)