From 383c9cb1705b6a0760a5208ff879a1cf56c35eb8 Mon Sep 17 00:00:00 2001 From: daniel1n <54945782+daniel1n@users.noreply.github.com> Date: Thu, 1 Jul 2021 18:25:51 +0800 Subject: [PATCH 1/5] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java的暴力解法(map记录频率) --- problems/0501.二叉搜索树中的众数.md | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index d2326d67..66be790f 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -345,6 +345,40 @@ public: Java: +暴力法 +```java +class Solution { + public int[] findMode(FindModeInBinarySearchTree.TreeNode root) { + Map map = new HashMap<>(); + List list = new ArrayList<>(); + if (root == null) return list.stream().mapToInt(Integer::intValue).toArray(); + // 获得频率 Map + searchBST(root, map); + List> mapList = map.entrySet().stream() + .sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue())) + .collect(Collectors.toList()); + list.add(mapList.get(0).getKey()); + // 把频率最高的加入 list + for (int i = 1; i < mapList.size(); i++) { + if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) { + list.add(mapList.get(i).getKey()); + } else { + break; + } + } + return list.stream().mapToInt(Integer::intValue).toArray(); + } + + void searchBST(FindModeInBinarySearchTree.TreeNode curr, Map map) { + if (curr == null) return; + map.put(curr.val, map.getOrDefault(curr.val, 0) + 1); + searchBST(curr.left, map); + searchBST(curr.right, map); + } + +} +``` + ```Java class Solution { ArrayList resList; From d5e128e20921517085aa199e3a0667e8c2c252de Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Thu, 1 Jul 2021 23:13:32 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200701.=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=20go=E7=89=88=EF=BC=88=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0701.二叉搜索树中的插入操作.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 794e0ae2..61027453 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -271,6 +271,9 @@ class Solution: Go: + +递归法 + ```Go func insertIntoBST(root *TreeNode, val int) *TreeNode { if root == nil { @@ -285,6 +288,31 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { return root } ``` +迭代法 +```go +func insertIntoBST(root *TreeNode, val int) *TreeNode { + if root == nil { + return &TreeNode{Val:val} + } + node := root + var pnode *TreeNode + for node != nil { + if val > node.Val { + pnode = node + node = node.Right + } else { + pnode = node + node = node.Left + } + } + if val > pnode.Val { + pnode.Right = &TreeNode{Val: val} + } else { + pnode.Left = &TreeNode{Val: val} + } + return root +} +``` JavaScript版本 From c47bafa412965064846d82be391a0ba851ae4d20 Mon Sep 17 00:00:00 2001 From: amdrose <927410375@qq.com> Date: Fri, 2 Jul 2021 01:47:13 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 31a808b0..5be94996 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -187,7 +187,23 @@ var twoSum = function (nums, target) { }; ``` +php +```php +function twoSum(array $nums, int $target): array +{ + for ($i = 0; $i < count($nums);$i++) { + // 计算剩下的数 + $residue = $target - $nums[$i]; + // 匹配的index,有则返回index, 无则返回false + $match_index = array_search($residue, $nums); + if ($match_index !== false && $match_index != $i) { + return array($i, $match_index); + } + } + return []; +} +``` ----------------------- From 46a4b5161cd5a777d6eb81547f2b25a7279248a5 Mon Sep 17 00:00:00 2001 From: amdrose <927410375@qq.com> Date: Fri, 2 Jul 2021 01:54:55 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 5b77a170..36abb58c 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -354,6 +354,47 @@ def is_valid(strs) end ``` +php: + +```php +function threeSum(array $nums): array +{ + $result = []; + $length = count($nums); + if ($length < 3) { + return []; + } + sort($nums); + for ($i = 0; $i < $length; $i++) { + // 如果大于0结束 + if ($nums[$i] > 0) break; + // 去重 + if ($i > 0 && $nums[$i] == $nums[$i - 1]) continue; + $left = $i + 1; + $right = $length - 1; + // 比较 + while ($left < $right) { + $sum = $nums[$i] + $nums[$left] + $nums[$right]; + if ($sum < 0) { + $left++; + } elseif ($sum > 0) { + $right--; + } else { + array_push($result, [$nums[$i], $nums[$left], $nums[$right]]); + while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++; + while ($left < $right && $nums[$right - 1] == $nums[$right]) $right--; + $left++; + $right--; + } + } + } + + return $result; +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 6360e80430bec5f648bf242ef1a2871a938d7189 Mon Sep 17 00:00:00 2001 From: hengzzha <80679845+hengzzha@users.noreply.github.com> Date: Fri, 2 Jul 2021 15:20:36 +0800 Subject: [PATCH 5/5] =?UTF-8?q?Update=20=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92-=E8=82=A1=E7=A5=A8=E9=97=AE=E9=A2=98=E6=80=BB?= =?UTF-8?q?=E7=BB=93=E7=AF=87.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/动态规划-股票问题总结篇.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md index cac77b65..d22132c0 100644 --- a/problems/动态规划-股票问题总结篇.md +++ b/problems/动态规划-股票问题总结篇.md @@ -381,6 +381,12 @@ dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); dp[i][2] = dp[i - 1][0] + prices[i]; dp[i][3] = dp[i - 1][2]; ``` +```C++ +dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3]- prices[i], dp[i - 1][1]) - prices[i]; +dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); +dp[i][2] = dp[i - 1][0] + prices[i]; +dp[i][3] = dp[i - 1][2]; +``` 整体代码如下: