From 3d6baa3ae3648ef41e7a96bd49cb68c3d9d2df2d Mon Sep 17 00:00:00 2001 From: dcj_hp <294487055@qq.com> Date: Fri, 20 May 2022 00:14:16 +0800 Subject: [PATCH 001/114] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20java=20=E4=B8=80?= =?UTF-8?q?=E7=BB=B4dp=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1143.最长公共子序列.md | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index ecedf89b..b4b8e6db 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -129,6 +129,9 @@ public: Java: ```java +/* + 二维dp数组 +*/ class Solution { public int longestCommonSubsequence(String text1, String text2) { int[][] dp = new int[text1.length() + 1][text2.length() + 1]; // 先对dp数组做初始化操作 @@ -146,6 +149,47 @@ class Solution { return dp[text1.length()][text2.length()]; } } + + + +/** + 一维dp数组 +*/ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int n1 = text1.length(); + int n2 = text2.length(); + + // 多从二维dp数组过程分析 + // 关键在于 如果记录 dp[i - 1][j - 1] + // 因为 dp[i - 1][j - 1] dp[j - 1] <=> dp[i][j - 1] + int [] dp = new int[n2 + 1]; + + for(int i = 1; i <= n1; i++){ + + // 这里pre相当于 dp[i - 1][j - 1] + int pre = dp[0]; + for(int j = 1; j <= n2; j++){ + + //用于给pre赋值 + int cur = dp[j]; + if(text1.charAt(i - 1) == text2.charAt(j - 1)){ + //这里pre相当于dp[i - 1][j - 1] 千万不能用dp[j - 1] !! + dp[j] = pre + 1; + } else{ + // dp[j] 相当于 dp[i - 1][j] + // dp[j - 1] 相当于 dp[i][j - 1] + dp[j] = Math.max(dp[j], dp[j - 1]); + } + + //更新dp[i - 1][j - 1], 为下次使用做准备 + pre = cur; + } + } + + return dp[n2]; + } +} ``` Python: From 2137778d44d256795bd0ca6c9b880b2c3be180d1 Mon Sep 17 00:00:00 2001 From: damon <245211612@qq.com> Date: Thu, 26 May 2022 00:29:02 +0800 Subject: [PATCH 002/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880015.?= =?UTF-8?q?=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=ACnsum=E7=9A=84?= =?UTF-8?q?=E9=80=9A=E7=94=A8=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index cc184c87..12d83d8f 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -345,6 +345,76 @@ var threeSum = function(nums) { return res }; ``` + +解法二:nSum通用解法。递归 + +```js +/** + * nsum通用解法,支持2sum,3sum,4sum...等等 + * 时间复杂度分析: + * 1. n = 2时,时间复杂度O(NlogN),排序所消耗的时间。、 + * 2. n > 2时,时间复杂度为O(N^n-1),即N的n-1次方,至少是2次方,此时可省略排序所消耗的时间。举例:3sum为O(n^2),4sum为O(n^3) + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + // nsum通用解法核心方法 + function nSumTarget(nums, n, start, target) { + // 前提:nums要先排序好 + let res = []; + if (n === 2) { + res = towSumTarget(nums, start, target); + } else { + for (let i = start; i < nums.length; i++) { + // 递归求(n - 1)sum + let subRes = nSumTarget( + nums, + n - 1, + i + 1, + target - nums[i] + ); + for (let j = 0; j < subRes.length; j++) { + res.push([nums[i], ...subRes[j]]); + } + // 跳过相同元素 + while (nums[i] === nums[i + 1]) i++; + } + } + return res; + } + + function towSumTarget(nums, start, target) { + // 前提:nums要先排序好 + let res = []; + let len = nums.length; + let left = start; + let right = len - 1; + while (left < right) { + let sum = nums[left] + nums[right]; + if (sum < target) { + while (nums[left] === nums[left + 1]) left++; + left++; + } else if (sum > target) { + while (nums[right] === nums[right - 1]) right--; + right--; + } else { + // 相等 + res.push([nums[left], nums[right]]); + // 跳过相同元素 + while (nums[left] === nums[left + 1]) left++; + while (nums[right] === nums[right - 1]) right--; + left++; + right--; + } + } + return res; + } + nums.sort((a, b) => a - b); + // n = 3,此时求3sum之和 + return nSumTarget(nums, 3, 0, 0); +}; +``` + TypeScript: ```typescript From c70440ac4318e48cfd3500ec290678f8976bb008 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 26 May 2022 10:54:39 +0800 Subject: [PATCH 003/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880283.?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E9=9B=B6.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 --- problems/0283.移动零.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index ed59d2c4..60eea378 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -133,6 +133,27 @@ var moveZeroes = function(nums) { }; ``` +TypeScript: + +```typescript +function moveZeroes(nums: number[]): void { + const length: number = nums.length; + let slowIndex: number = 0, + fastIndex: number = 0; + while (fastIndex < length) { + if (nums[fastIndex] !== 0) { + nums[slowIndex++] = nums[fastIndex]; + }; + fastIndex++; + } + while (slowIndex < length) { + nums[slowIndex++] = 0; + } +}; +``` + + + -----------------------
From 36a1d71201a59a113a339a1555e2734b7e0cc764 Mon Sep 17 00:00:00 2001 From: Harrytsz <18810271846@163.com> Date: Thu, 26 May 2022 16:35:42 +0800 Subject: [PATCH 004/114] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去掉 continue,增加 else 分支,逻辑更加清晰 --- problems/0541.反转字符串II.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 8c13a390..38dc6853 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -53,10 +53,10 @@ public: // 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符 if (i + k <= s.size()) { reverse(s.begin() + i, s.begin() + i + k ); - continue; + } else { + // 3. 剩余字符少于 k 个,则将剩余字符全部反转。 + reverse(s.begin() + i, s.end()); } - // 3. 剩余字符少于 k 个,则将剩余字符全部反转。 - reverse(s.begin() + i, s.begin() + s.size()); } return s; } From 8d3e5b46083fe83210b683aec67e471a4a00225e Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Thu, 26 May 2022 13:09:05 +0800 Subject: [PATCH 005/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0232.=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md)=EF=BC=9APHP?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1a56d9f3..6752651e 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -496,5 +496,49 @@ void myQueueFree(MyQueue* obj) { } ``` +PHP: +```php +// SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8] +// https://www.php.net/manual/zh/class.splstack.php +class MyQueue { + // 双栈模拟队列:In栈存储数据;Out栈辅助处理 + private $stackIn; + private $stackOut; + + function __construct() { + $this->stackIn = new SplStack(); + $this->stackOut = new SplStack(); + } + + // In: 1 2 3 <= push + function push($x) { + $this->stackIn->push($x); + } + + function pop() { + $this->peek(); + return $this->stackOut->pop(); + } + + function peek() { + if($this->stackOut->isEmpty()){ + $this->shift(); + } + return $this->stackOut->top(); + } + + function empty() { + return $this->stackOut->isEmpty() && $this->stackIn->isEmpty(); + } + + // 如果Out栈为空,把In栈数据压入Out栈 + // In: 1 2 3 => pop push => 1 2 3 :Out + private function shift(){ + while(!$this->stackIn->isEmpty()){ + $this->stackOut->push($this->stackIn->pop()); + } + } +} +``` -----------------------
From 5baecc0f7919912e10ea64651ddc880f8c61f3c9 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Fri, 27 May 2022 00:59:37 +0800 Subject: [PATCH 006/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md)=EF=BC=9APHP?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 78 +++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 3457c4b3..f946cd86 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -816,5 +816,83 @@ class MyStack { } ``` +PHP +> 双对列 +```php +// SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8) +// https://www.php.net/manual/zh/class.splqueue.php +class MyStack { + public $queueMain; // 保存数据 + public $queueTmp; // 辅助作用 + + function __construct() { + $this->queueMain=new SplQueue(); + $this->queueTmp=new SplQueue(); + } + + // queueMain: 1,2,3 <= add + function push($x) { + $this->queueMain->enqueue($x); + } + + function pop() { + $qmSize = $this->queueMain->Count(); + $qmSize --; + // queueMain: 3,2,1 => pop =>2,1 => add => 2,1 :queueTmp + while($qmSize --){ + $this->queueTmp->enqueue($this->queueMain->dequeue()); + } + // queueMain: 3 + $val = $this->queueMain->dequeue(); + // queueMain <= queueTmp + $this->queueMain = $this->queueTmp; + // 清空queueTmp,下次使用 + $this->queueTmp = new SplQueue(); + return $val; + } + + function top() { + // 底层是双链表实现:从双链表的末尾查看节点 + return $this->queueMain->top(); + } + + function empty() { + return $this->queueMain->isEmpty(); + } +} +``` +> 单对列 +```php +class MyStack { + public $queue; + + function __construct() { + $this->queue=new SplQueue(); + } + + function push($x) { + $this->queue->enqueue($x); + } + + function pop() { + $qmSize = $this->queue->Count(); + $qmSize --; + //queue: 3,2,1 => pop =>2,1 => add => 2,1,3 :queue + while($qmSize --){ + $this->queue->enqueue($this->queue->dequeue()); + } + $val = $this->queue->dequeue(); + return $val; + } + + function top() { + return $this->queue->top(); + } + + function empty() { + return $this->queue->isEmpty(); + } +} +``` -----------------------
From 513ec5418910aee18a1b6722b9316ffaaeb2dec4 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 27 May 2022 12:18:24 +0800 Subject: [PATCH 007/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880189.?= =?UTF-8?q?=E6=97=8B=E8=BD=AC=E6=95=B0=E7=BB=84.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0189.旋转数组.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index 3ffed877..23092f9c 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -7,6 +7,8 @@ # 189. 旋转数组 +[力扣题目链接](https://leetcode.cn/problems/rotate-array/) + 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 进阶: @@ -160,6 +162,27 @@ var rotate = function (nums, k) { }; ``` +## TypeScript + +```typescript +function rotate(nums: number[], k: number): void { + const length: number = nums.length; + k %= length; + reverseByRange(nums, 0, length - 1); + reverseByRange(nums, 0, k - 1); + reverseByRange(nums, k, length - 1); +}; +function reverseByRange(nums: number[], left: number, right: number): void { + while (left < right) { + const temp = nums[left]; + nums[left] = nums[right]; + nums[right] = temp; + left++; + right--; + } +} +``` + ----------------------- From 156d8fdd92b07c6ba74ed68ee9e8cd092c106024 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Fri, 27 May 2022 14:21:41 +0800 Subject: [PATCH 008/114] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md)=EF=BC=9APHP=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0020.有效的括号.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 7bb7f746..5e468549 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -401,5 +401,33 @@ bool isValid(char * s){ } ``` +PHP: +```php +// https://www.php.net/manual/zh/class.splstack.php +class Solution +{ + function isValid($s){ + $stack = new SplStack(); + for ($i = 0; $i < strlen($s); $i++) { + if ($s[$i] == "(") { + $stack->push(')'); + } else if ($s[$i] == "{") { + $stack->push('}'); + } else if ($s[$i] == "[") { + $stack->push(']'); + // 2、遍历匹配过程中,发现栈内没有要匹配的字符 return false + // 3、遍历匹配过程中,栈已为空,没有匹配的字符了,说明右括号没有找到对应的左括号 return false + } else if ($stack->isEmpty() || $stack->top() != $s[$i]) { + return false; + } else {//$stack->top() == $s[$i] + $stack->pop(); + } + } + // 1、遍历完,但是栈不为空,说明有相应的括号没有被匹配,return false + return $stack->isEmpty(); + } +} +``` + -----------------------
From edbff2f4f4746b09afa2d6dd8981f000d0bc75ae Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Fri, 27 May 2022 15:04:23 +0800 Subject: [PATCH 009/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(1047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md)?= =?UTF-8?q?=EF=BC=9Aphp=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1047.删除字符串中的所有相邻重复项.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 638c8f4e..65428394 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -375,5 +375,30 @@ func removeDuplicates(_ s: String) -> String { } ``` +PHP: +```php +class Solution { + function removeDuplicates($s) { + $stack = new SplStack(); + for($i=0;$iisEmpty() || $s[$i] != $stack->top()){ + $stack->push($s[$i]); + }else{ + $stack->pop(); + } + } + + $result = ""; + while(!$stack->isEmpty()){ + $result.= $stack->top(); + $stack->pop(); + } + + // 此时字符串需要反转一下 + return strrev($result); + } +} +``` + -----------------------
From 821ca656e14384fd63e937a0cd99fbe7bde813f0 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 27 May 2022 16:10:35 +0800 Subject: [PATCH 010/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0654.最大二叉树.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index 1c73354b..1b0eac66 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -476,7 +476,33 @@ func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? { } ``` +## Scala +```scala +object Solution { + def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = { + if (nums.size == 0) return null + // 找到数组最大值 + var maxIndex = 0 + var maxValue = Int.MinValue + for (i <- nums.indices) { + if (nums(i) > maxValue) { + maxIndex = i + maxValue = nums(i) + } + } + + // 构建一棵树 + var root = new TreeNode(maxValue, null, null) + + // 递归寻找左右子树 + root.left = constructMaximumBinaryTree(nums.slice(0, maxIndex)) + root.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1, nums.length)) + + root // 返回root + } +} +``` -----------------------
From de34170f5cc1526eb0c4b1860943a57e6f41c4e1 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 27 May 2022 16:37:53 +0800 Subject: [PATCH 011/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200617.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 55786ea9..d8bdc91c 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -631,7 +631,60 @@ function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode | }; ``` +## Scala +递归: +```scala +object Solution { + def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = { + if (root1 == null) return root2 // 如果root1为空,返回root2 + if (root2 == null) return root1 // 如果root2为空,返回root1 + // 新建一个节点,值为两个节点的和 + var node = new TreeNode(root1.value + root2.value) + // 往下递归 + node.left = mergeTrees(root1.left, root2.left) + node.right = mergeTrees(root1.right, root2.right) + node // 返回node,return关键字可以省略 + } +} +``` + +迭代: +```scala +object Solution { + import scala.collection.mutable + def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = { + if (root1 == null) return root2 + if (root2 == null) return root1 + var stack = mutable.Stack[TreeNode]() + // 先放node2再放node1 + stack.push(root2) + stack.push(root1) + while (!stack.isEmpty) { + var node1 = stack.pop() + var node2 = stack.pop() + node1.value += node2.value + if (node1.right != null && node2.right != null) { + stack.push(node2.right) + stack.push(node1.right) + } else { + if(node1.right == null){ + node1.right = node2.right + } + } + if (node1.left != null && node2.left != null) { + stack.push(node2.left) + stack.push(node1.left) + } else { + if(node1.left == null){ + node1.left = node2.left + } + } + } + root1 + } +} +``` ----------------------- From 9412e2e40529db526bbb16de1f3a9c45cf2c3949 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 27 May 2022 17:01:56 +0800 Subject: [PATCH 012/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200700.=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=90=9C?= =?UTF-8?q?=E7=B4=A2.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 40cf4ea1..16ddef9b 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -363,7 +363,34 @@ function searchBST(root: TreeNode | null, val: number): TreeNode | null { }; ``` +## Scala +递归: +```scala +object Solution { + def searchBST(root: TreeNode, value: Int): TreeNode = { + if (root == null || value == root.value) return root + // 相当于三元表达式,在Scala中if...else有返回值 + if (value < root.value) searchBST(root.left, value) else searchBST(root.right, value) + } +} +``` + +迭代: +```scala +object Solution { + def searchBST(root: TreeNode, value: Int): TreeNode = { + // 因为root是不可变量,所以需要赋值给一个可变量 + var node = root + while (node != null) { + if (value < node.value) node = node.left + else if (value > node.value) node = node.right + else return node + } + null // 没有返回就返回空 + } +} +``` -----------------------
From d4a4eda4b8396603a70871ff215b48b2d17eef93 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 27 May 2022 17:29:44 +0800 Subject: [PATCH 013/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Sca?= =?UTF-8?q?la=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index a8f3c324..06f1b8d1 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -589,7 +589,50 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` +## Scala +辅助数组解决: +```scala +object Solution { + import scala.collection.mutable + def isValidBST(root: TreeNode): Boolean = { + var arr = new mutable.ArrayBuffer[Int]() + // 递归中序遍历二叉树,将节点添加到arr + def traversal(node: TreeNode): Unit = { + if (node == null) return + traversal(node.left) + arr.append(node.value) + traversal(node.right) + } + traversal(root) + // 这个数组如果是升序就代表是二叉搜索树 + for (i <- 1 until arr.size) { + if (arr(i) <= arr(i - 1)) return false + } + true + } +} +``` + +递归中解决: +```scala +object Solution { + def isValidBST(root: TreeNode): Boolean = { + var flag = true + var preValue:Long = Long.MinValue // 这里要使用Long类型 + + def traversal(node: TreeNode): Unit = { + if (node == null || flag == false) return + traversal(node.left) + if (node.value > preValue) preValue = node.value + else flag = false + traversal(node.right) + } + traversal(root) + flag + } +} +``` -----------------------
From 4e95d663a047d1745c57b3f46cba055836e8ec13 Mon Sep 17 00:00:00 2001 From: damon <245211612@qq.com> Date: Fri, 27 May 2022 22:52:57 +0800 Subject: [PATCH 014/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.?= =?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=B0O(1)=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 1d17784d..7c899195 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -234,6 +234,7 @@ func fib(n int) int { } ``` ### Javascript +解法一 ```Javascript var fib = function(n) { let dp = [0, 1] @@ -244,6 +245,23 @@ var fib = function(n) { return dp[n] }; ``` +解法二:时间复杂度O(N),空间复杂度O(1) +```Javascript +var fib = function(n) { + // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1) + let pre1 = 1 + let pre2 = 0 + let temp + if (n === 0) return 0 + if (n === 1) return 1 + for(let i = 2; i <= n; i++) { + temp = pre1 + pre1 = pre1 + pre2 + pre2 = temp + } + return pre1 +}; +``` TypeScript From 992909385fd8bc0181a76bf3fa65eb10f9885190 Mon Sep 17 00:00:00 2001 From: damon <245211612@qq.com> Date: Fri, 27 May 2022 22:52:57 +0800 Subject: [PATCH 015/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.?= =?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E5=88=B0O(1)=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 1d17784d..7c899195 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -234,6 +234,7 @@ func fib(n int) int { } ``` ### Javascript +解法一 ```Javascript var fib = function(n) { let dp = [0, 1] @@ -244,6 +245,23 @@ var fib = function(n) { return dp[n] }; ``` +解法二:时间复杂度O(N),空间复杂度O(1) +```Javascript +var fib = function(n) { + // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1) + let pre1 = 1 + let pre2 = 0 + let temp + if (n === 0) return 0 + if (n === 1) return 1 + for(let i = 2; i <= n; i++) { + temp = pre1 + pre1 = pre1 + pre2 + pre2 = temp + } + return pre1 +}; +``` TypeScript From a9f830267bdcd9a5bc6ec19dfbc1b4436fa41a1e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 28 May 2022 11:12:53 +0800 Subject: [PATCH 016/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880724.?= =?UTF-8?q?=E5=AF=BB=E6=89=BE=E6=95=B0=E7=BB=84=E7=9A=84=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E7=B4=A2=E5=BC=95.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0724.寻找数组的中心索引.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0724.寻找数组的中心索引.md b/problems/0724.寻找数组的中心索引.md index 14dcd2c0..0052e5d4 100644 --- a/problems/0724.寻找数组的中心索引.md +++ b/problems/0724.寻找数组的中心索引.md @@ -140,6 +140,24 @@ var pivotIndex = function(nums) { }; ``` +### TypeScript + +```typescript +function pivotIndex(nums: number[]): number { + const length: number = nums.length; + const sum: number = nums.reduce((a, b) => a + b); + let leftSum: number = 0; + for (let i = 0; i < length; i++) { + const rightSum: number = sum - leftSum - nums[i]; + if (leftSum === rightSum) return i; + leftSum += nums[i]; + } + return -1; +}; +``` + + + -----------------------
From 6f46d91676693a83c0620e49bfdf9b9a252d87a0 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 28 May 2022 13:55:52 +0800 Subject: [PATCH 017/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0530.二叉搜索树的最小绝对差.md | 77 +++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 77699c9f..d7db85ae 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -431,7 +431,84 @@ function getMinimumDifference(root: TreeNode | null): number { }; ``` +## Scala +构建二叉树的有序数组: + +```scala +object Solution { + import scala.collection.mutable + def getMinimumDifference(root: TreeNode): Int = { + val arr = mutable.ArrayBuffer[Int]() + def traversal(node: TreeNode): Unit = { + if (node == null) return + traversal(node.left) + arr.append(node.value) + traversal(node.right) + } + traversal(root) + // 在有序数组上求最小差值 + var result = Int.MaxValue + for (i <- 1 until arr.size) { + result = math.min(result, arr(i) - arr(i - 1)) + } + result // 返回最小差值 + } +} +``` + +递归记录前一个节点: + +```scala +object Solution { + def getMinimumDifference(root: TreeNode): Int = { + var result = Int.MaxValue // 初始化为最大值 + var pre: TreeNode = null // 记录前一个节点 + + def traversal(cur: TreeNode): Unit = { + if (cur == null) return + traversal(cur.left) + if (pre != null) { + // 对比result与节点之间的差值 + result = math.min(result, cur.value - pre.value) + } + pre = cur + traversal(cur.right) + } + + traversal(root) + result // return关键字可以省略 + } +} +``` + +迭代解决: + +```scala +object Solution { + import scala.collection.mutable + def getMinimumDifference(root: TreeNode): Int = { + var result = Int.MaxValue // 初始化为最大值 + var pre: TreeNode = null // 记录前一个节点 + var cur = root + var stack = mutable.Stack[TreeNode]() + while (cur != null || !stack.isEmpty) { + if (cur != null) { + stack.push(cur) + cur = cur.left + } else { + cur = stack.pop() + if (pre != null) { + result = math.min(result, cur.value - pre.value) + } + pre = cur + cur = cur.right + } + } + result // return关键字可以省略 + } +} +``` -----------------------
From 779adf74cb1ec9b1f8b17db3cbf3d8fc3b88392f Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 28 May 2022 14:53:40 +0800 Subject: [PATCH 018/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 71 ++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 9cb5d071..c08f68d9 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -9,7 +9,7 @@ # 501.二叉搜索树中的众数 -[力扣题目链接](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/) +[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/) 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。 @@ -798,7 +798,76 @@ function findMode(root: TreeNode | null): number[] { }; ``` +## Scala +暴力: +```scala +object Solution { + // 导包 + import scala.collection.mutable // 集合包 + import scala.util.control.Breaks.{break, breakable} // 流程控制包 + def findMode(root: TreeNode): Array[Int] = { + var map = mutable.HashMap[Int, Int]() // 存储节点的值,和该值出现的次数 + def searchBST(curNode: TreeNode): Unit = { + if (curNode == null) return + var value = map.getOrElse(curNode.value, 0) + map.put(curNode.value, value + 1) + searchBST(curNode.left) + searchBST(curNode.right) + } + searchBST(root) // 前序遍历把每个节点的值加入到里面 + // 将map转换为list,随后根据元组的第二个值进行排序 + val list = map.toList.sortWith((map1, map2) => { + if (map1._2 > map2._2) true else false + }) + var res = mutable.ArrayBuffer[Int]() + res.append(list(0)._1) // 将第一个加入结果集 + breakable { + for (i <- 1 until list.size) { + // 如果值相同就加入结果集合,反之break + if (list(i)._2 == list(0)._2) res.append(list(i)._1) + else break + } + } + res.toArray // 最终返回res的Array格式,return关键字可以省略 + } +} +``` + +递归(利用二叉搜索树的性质): +```scala +object Solution { + import scala.collection.mutable + def findMode(root: TreeNode): Array[Int] = { + var maxCount = 0 // 最大频率 + var count = 0 // 统计频率 + var pre: TreeNode = null + var result = mutable.ArrayBuffer[Int]() + + def searchBST(cur: TreeNode): Unit = { + if (cur == null) return + searchBST(cur.left) + if (pre == null) count = 1 // 等于空置为1 + else if (pre.value == cur.value) count += 1 // 与上一个节点的值相同加1 + else count = 1 // 与上一个节点的值不同 + pre = cur + + // 如果和最大值相同,则放入结果集 + if (count == maxCount) result.append(cur.value) + + // 如果当前计数大于最大值频率,更新最大值,清空结果集 + if (count > maxCount) { + maxCount = count + result.clear() + result.append(cur.value) + } + searchBST(cur.right) + } + searchBST(root) + result.toArray // return关键字可以省略 + } +} +``` ----------------------- From 95cc2b965807a6493fe64eb23d6bbfa66587f0fa Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 29 May 2022 10:18:40 +0800 Subject: [PATCH 019/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200236.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0236.二叉树的最近公共祖先.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 69a6d0d6..a99180c3 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` +## Scala +```scala +object Solution { + def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = { + // 递归结束条件 + if (root == null || root == p || root == q) { + return root + } + + var left = lowestCommonAncestor(root.left, p, q) + var right = lowestCommonAncestor(root.right, p, q) + + if (left != null && right != null) return root + if (left == null) return right + left + } +} +``` -----------------------
From 78930cdd0970c0850db6aa7c90ced0510fd6b1ec Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 29 May 2022 10:32:47 +0800 Subject: [PATCH 020/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0235.二叉搜索树的最近公共祖先.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index f7f1427a..a0f30999 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -381,7 +381,36 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` +## Scala +递归: + +```scala +object Solution { + def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = { + // scala中每个关键字都有其返回值,于是可以不写return + if (root.value > p.value && root.value > q.value) lowestCommonAncestor(root.left, p, q) + else if (root.value < p.value && root.value < q.value) lowestCommonAncestor(root.right, p, q) + else root + } +} +``` + +迭代: + +```scala +object Solution { + def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = { + var curNode = root // 因为root是不可变量,所以要赋值给curNode一个可变量 + while(curNode != null){ + if(curNode.value > p.value && curNode.value > q.value) curNode = curNode.left + else if(curNode.value < p.value && curNode.value < q.value) curNode = curNode.right + else return curNode + } + null + } +} +``` ----------------------- From 45a01eed32598a6cf4c9a20d995aa46efe296dd5 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 29 May 2022 13:04:37 +0800 Subject: [PATCH 021/114] =?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.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 50e39ade..135911b9 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -585,6 +585,43 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { ``` +## Scala + +递归: + +```scala +object Solution { + def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = { + if (root == null) return new TreeNode(`val`) + if (`val` < root.value) root.left = insertIntoBST(root.left, `val`) + else root.right = insertIntoBST(root.right, `val`) + root // 返回根节点 + } +} +``` + +迭代: + +```scala +object Solution { + def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = { + if (root == null) { + return new TreeNode(`val`) + } + var parent = root // 记录当前节点的父节点 + var curNode = root + while (curNode != null) { + parent = curNode + if(`val` < curNode.value) curNode = curNode.left + else curNode = curNode.right + } + if(`val` < parent.value) parent.left = new TreeNode(`val`) + else parent.right = new TreeNode(`val`) + root // 最终返回根节点 + } +} +``` + -----------------------
From cc72b164cf7780f3dc4e119d30033dd0708eddd5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 29 May 2022 22:48:13 +0800 Subject: [PATCH 022/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880034.?= =?UTF-8?q?=E5=9C=A8=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=92=8C=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...排序数组中查找元素的第一个和最后一个位置.md | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index dfd90b82..25db0083 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -481,6 +481,61 @@ var searchRange = function(nums, target) { }; ``` +### TypeScript + +```typescript +function searchRange(nums: number[], target: number): number[] { + const leftBoard: number = getLeftBorder(nums, target); + const rightBoard: number = getRightBorder(nums, target); + // target 在nums区间左侧或右侧 + if (leftBoard === (nums.length - 1) || rightBoard === 0) return [-1, -1]; + // target 不存在与nums范围内 + if (rightBoard - leftBoard <= 1) return [-1, -1]; + // target 存在于nums范围内 + return [leftBoard + 1, rightBoard - 1]; +}; +// 查找第一个大于target的元素下标 +function getRightBorder(nums: number[], target: number): number { + let left: number = 0, + right: number = nums.length - 1; + // 0表示target在nums区间的左边 + let rightBoard: number = 0; + while (left <= right) { + let mid = Math.floor((left + right) / 2); + if (nums[mid] <= target) { + // 右边界一定在mid右边(不含mid) + left = mid + 1; + rightBoard = left; + } else { + // 右边界在mid左边(含mid) + right = mid - 1; + } + } + return rightBoard; +} +// 查找第一个小于target的元素下标 +function getLeftBorder(nums: number[], target: number): number { + let left: number = 0, + right: number = nums.length - 1; + // length-1表示target在nums区间的右边 + let leftBoard: number = nums.length - 1; + while (left <= right) { + let mid = Math.floor((left + right) / 2); + if (nums[mid] >= target) { + // 左边界一定在mid左边(不含mid) + right = mid - 1; + leftBoard = right; + } else { + // 左边界在mid右边(含mid) + left = mid + 1; + } + } + return leftBoard; +} +``` + + + -----------------------
From 8eb956b553b178d3d14555c270f8b726baea5870 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 30 May 2022 10:29:31 +0800 Subject: [PATCH 023/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200450.=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=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0450.删除二叉搜索树中的节点.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index e8f7e54c..8367a145 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -582,7 +582,35 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null { }; ``` +## Scala +```scala +object Solution { + def deleteNode(root: TreeNode, key: Int): TreeNode = { + if (root == null) return root // 第一种情况,没找到删除的节点,遍历到空节点直接返回 + if (root.value == key) { + // 第二种情况: 左右孩子都为空,直接删除节点,返回null + if (root.left == null && root.right == null) return null + // 第三种情况: 左孩子为空,右孩子不为空,右孩子补位 + else if (root.left == null && root.right != null) return root.right + // 第四种情况: 左孩子不为空,右孩子为空,左孩子补位 + else if (root.left != null && root.right == null) return root.left + // 第五种情况: 左右孩子都不为空,将删除节点的左子树头节点(左孩子)放到 + // 右子树的最左边节点的左孩子上,返回删除节点的右孩子 + else { + var tmp = root.right + while (tmp.left != null) tmp = tmp.left + tmp.left = root.left + return root.right + } + } + if (root.value > key) root.left = deleteNode(root.left, key) + if (root.value < key) root.right = deleteNode(root.right, key) + + root // 返回根节点,return关键字可以省略 + } +} +``` -----------------------
From df2ff8cc2e77261a0360ac848949c233f3c6258a Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 30 May 2022 10:50:51 +0800 Subject: [PATCH 024/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200669.=E4=BF=AE?= =?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Sca?= =?UTF-8?q?la=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0669.修剪二叉搜索树.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 385b2268..988bff30 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -453,7 +453,21 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n }; ``` +## Scala +递归法: +```scala +object Solution { + def trimBST(root: TreeNode, low: Int, high: Int): TreeNode = { + if (root == null) return null + if (root.value < low) return trimBST(root.right, low, high) + if (root.value > high) return trimBST(root.left, low, high) + root.left = trimBST(root.left, low, high) + root.right = trimBST(root.right, low, high) + root + } +} +``` ----------------------- From 43d450d8271b6af68d209bda8f22ed71838418f1 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 30 May 2022 11:20:33 +0800 Subject: [PATCH 025/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880922.?= =?UTF-8?q?=E6=8C=89=E5=A5=87=E5=81=B6=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84?= =?UTF-8?q?II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0922.按奇偶排序数组II.md | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md index cb564fb6..cb8cec73 100644 --- a/problems/0922.按奇偶排序数组II.md +++ b/problems/0922.按奇偶排序数组II.md @@ -260,6 +260,75 @@ var sortArrayByParityII = function(nums) { }; ``` +### TypeScript + +> 方法一: + +```typescript +function sortArrayByParityII(nums: number[]): number[] { + const evenArr: number[] = [], + oddArr: number[] = []; + for (let num of nums) { + if (num % 2 === 0) { + evenArr.push(num); + } else { + oddArr.push(num); + } + } + const resArr: number[] = []; + for (let i = 0, length = nums.length / 2; i < length; i++) { + resArr.push(evenArr[i]); + resArr.push(oddArr[i]); + } + return resArr; +}; +``` + +> 方法二: + +```typescript +function sortArrayByParityII(nums: number[]): number[] { + const length: number = nums.length; + const resArr: number[] = []; + let evenIndex: number = 0, + oddIndex: number = 1; + for (let i = 0; i < length; i++) { + if (nums[i] % 2 === 0) { + resArr[evenIndex] = nums[i]; + evenIndex += 2; + } else { + resArr[oddIndex] = nums[i]; + oddIndex += 2; + } + } + return resArr; +}; +``` + +> 方法三: + +```typescript +function sortArrayByParityII(nums: number[]): number[] { + const length: number = nums.length; + let oddIndex: number = 1; + for (let evenIndex = 0; evenIndex < length; evenIndex += 2) { + if (nums[evenIndex] % 2 === 1) { + // 在偶数位遇到了奇数 + while (oddIndex < length && nums[oddIndex] % 2 === 1) { + oddIndex += 2; + } + // 在奇数位遇到了偶数,交换 + let temp = nums[evenIndex]; + nums[evenIndex] = nums[oddIndex]; + nums[oddIndex] = temp; + } + } + return nums; +}; +``` + + + -----------------------
From 3e885d3aff0b2c826fe522d7eb5d2cd039b62f13 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Mon, 30 May 2022 11:25:52 +0800 Subject: [PATCH 026/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200108.=E5=B0=86?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Scala?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0108.将有序数组转换为二叉搜索树.md | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 6ee3947b..493118a6 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -448,5 +448,27 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { } ``` +## Scala + +递归: + +```scala +object Solution { + def sortedArrayToBST(nums: Array[Int]): TreeNode = { + def buildTree(left: Int, right: Int): TreeNode = { + if (left > right) return null // 当left大于right的时候,返回空 + // 最中间的节点是当前节点 + var mid = left + (right - left) / 2 + var curNode = new TreeNode(nums(mid)) + curNode.left = buildTree(left, mid - 1) + curNode.right = buildTree(mid + 1, right) + curNode + } + buildTree(0, nums.size - 1) + } +} +``` + + -----------------------
From d4b5abfe9bc2f32f99c92a1d3794b58d78c69141 Mon Sep 17 00:00:00 2001 From: Ezralin <10881430+ezralin@user.noreply.gitee.com> Date: Tue, 31 May 2022 08:41:53 +0800 Subject: [PATCH 027/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704.=E4=BA=8C?= =?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE.md=20Kotlin=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0704.二分查找.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 55625130..3dcbd175 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -611,6 +611,36 @@ public class Solution{ } ``` +**Kotlin:** +```kotlin +class Solution { + fun search(nums: IntArray, target: Int): Int { + // leftBorder + var left:Int = 0 + // rightBorder + var right:Int = nums.size - 1 + // 使用左闭右闭区间 + while (left <= right) { + var middle:Int = left + (right - left)/2 + // taget 在左边 + if (nums[middle] > target) { + right = middle - 1 + } + else { + // target 在右边 + if (nums[middle] < target) { + left = middle + 1 + } + // 找到了,返回 + else return middle + } + } + // 没找到,返回 + return -1 + } +} +``` + -----------------------
From ab5b82969f30b767f2815883272f4b3a49ad0c82 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Tue, 31 May 2022 15:56:00 +0800 Subject: [PATCH 028/114] =?UTF-8?q?=E6=96=B0=E5=A2=9E(0150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?.md)=EF=BC=9Aphp=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index fd3d69aa..7e142ba3 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -326,5 +326,29 @@ func evalRPN(_ tokens: [String]) -> Int { } ``` +PHP: +```php +class Solution { + function evalRPN($tokens) { + $st = new SplStack(); + for($i = 0;$ipush($tokens[$i]); + }else{ + // 是符号进行运算 + $num1 = $st->pop(); + $num2 = $st->pop(); + if ($tokens[$i] == "+") $st->push($num2 + $num1); + if ($tokens[$i] == "-") $st->push($num2 - $num1); + if ($tokens[$i] == "*") $st->push($num2 * $num1); + // 注意处理小数部分 + if ($tokens[$i] == "/") $st->push(intval($num2 / $num1)); + } + } + return $st->pop(); + } +} +``` -----------------------
From ca18b7d1f5c31a91c262ec4a1cf0b93cc00b3cf1 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 31 May 2022 22:13:58 +0800 Subject: [PATCH 029/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200538.=E6=8A=8A?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=E6=A0=91.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0538.把二叉搜索树转换为累加树.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 37eb7d0f..13637784 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -352,6 +352,24 @@ function convertBST(root: TreeNode | null): TreeNode | null { }; ``` +## Scala + +```scala +object Solution { + def convertBST(root: TreeNode): TreeNode = { + var sum = 0 + def convert(node: TreeNode): Unit = { + if (node == null) return + convert(node.right) + sum += node.value + node.value = sum + convert(node.left) + } + convert(root) + root + } +} +``` ----------------------- From 752cfda89351cc2d54897a6309a181f197cc6c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Tue, 31 May 2022 23:46:41 +0800 Subject: [PATCH 030/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 3a93ac88..5e7742ee 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -329,5 +329,20 @@ int removeElement(int* nums, int numsSize, int val){ } ``` +C#: +```csharp +public class Solution { + public int RemoveElement(int[] nums, int val) { + int slow = 0; + for (int fast = 0; fast < nums.Length; fast++) { + if (val != nums[fast]) { + nums[slow++] = nums[fast]; + } + } + return slow; + } +} +``` + -----------------------
From f51d72b71baf6698d2078dd9b2ce5432aa7cfe41 Mon Sep 17 00:00:00 2001 From: SevenMonths Date: Wed, 1 Jun 2022 18:45:16 +0800 Subject: [PATCH 031/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md)?= =?UTF-8?q?=EF=BC=9APHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index f269450f..227ab71c 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -631,5 +631,82 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { } ``` +PHP: +```php +class Solution { + /** + * @param Integer[] $nums + * @param Integer $k + * @return Integer[] + */ + function maxSlidingWindow($nums, $k) { + $myQueue = new MyQueue(); + // 先将前k的元素放进队列 + for ($i = 0; $i < $k; $i++) { + $myQueue->push($nums[$i]); + } + + $result = []; + $result[] = $myQueue->max(); // result 记录前k的元素的最大值 + + for ($i = $k; $i < count($nums); $i++) { + $myQueue->pop($nums[$i - $k]); // 滑动窗口移除最前面元素 + $myQueue->push($nums[$i]); // 滑动窗口前加入最后面的元素 + $result[]= $myQueue->max(); // 记录对应的最大值 + } + return $result; + } + +} + +// 单调对列构建 +class MyQueue{ + private $queue; + + public function __construct(){ + $this->queue = new SplQueue(); //底层是双向链表实现。 + } + + public function pop($v){ + // 判断当前对列是否为空 + // 比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。 + // bottom 从链表前端查看元素, dequeue 从双向链表的开头移动一个节点 + if(!$this->queue->isEmpty() && $v == $this->queue->bottom()){ + $this->queue->dequeue(); //弹出队列 + } + } + + public function push($v){ + // 判断当前对列是否为空 + // 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。 + // 这样就保持了队列里的数值是单调从大到小的了。 + while (!$this->queue->isEmpty() && $v > $this->queue->top()) { + $this->queue->pop(); // pop从链表末尾弹出一个元素, + } + $this->queue->enqueue($v); + } + + // 查询当前队列里的最大值 直接返回队首 + public function max(){ + // bottom 从链表前端查看元素, top从链表末尾查看元素 + return $this->queue->bottom(); + } + + // 辅助理解: 打印队列元素 + public function println(){ + // "迭代器移动到链表头部": 可理解为从头遍历链表元素做准备。 + // 【PHP中没有指针概念,所以就没说指针。从数据结构上理解,就是把指针指向链表头部】 + $this->queue->rewind(); + + echo "Println: "; + while($this->queue->valid()){ + echo $this->queue->current()," -> "; + $this->queue->next(); + } + echo "\n"; + } +} +``` + -----------------------
From 90075121ecd61558cb50d80668298fb4263c6cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Wed, 1 Jun 2022 19:47:53 +0800 Subject: [PATCH 032/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0e79a3d6..f1b0e4ec 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -394,6 +394,24 @@ object Solution { } ``` - +C#: +```csharp +public class Solution { + public int[] SortedSquares(int[] nums) { + int k = nums.Length - 1; + int[] result = new int[nums.Length]; + for (int i = 0, j = nums.Length - 1;i <= j;){ + if (nums[i] * nums[i] < nums[j] * nums[j]) { + result[k--] = nums[j] * nums[j]; + j--; + } else { + result[k--] = nums[i] * nums[i]; + i++; + } + } + return result; + } +} +``` -----------------------
From 0bdb37d7fa0138c6870b882a8577146ad43a01ca Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 2 Jun 2022 10:05:46 +0800 Subject: [PATCH 033/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 9e0398ab..f280c176 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -673,5 +673,33 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { } ``` +### Scala + +```scala +object Solution { + import scala.collection.mutable // 导包 + def combine(n: Int, k: Int): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() // 存放结果集 + var path = mutable.ListBuffer[Int]() //存放符合条件的结果 + + def backtracking(n: Int, k: Int, startIndex: Int): Unit = { + if (path.size == k) { + // 如果path的size == k就达到题目要求,添加到结果集,并返回 + result.append(path.toList) + return + } + for (i <- startIndex to n) { // 遍历从startIndex到n + path.append(i) // 先把数字添加进去 + backtracking(n, k, i + 1) // 进行下一步回溯 + path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字 + } + } + + backtracking(n, k, 1) // 执行回溯 + result.toList // 最终返回result的List形式,return关键字可以省略 + } +} +``` + -----------------------
From e734d0a12265ac455691d5cd15efe2c7fbfb8491 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 2 Jun 2022 10:18:52 +0800 Subject: [PATCH 034/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=20=E5=89=AA=E6=9E=9D=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index f280c176..58382221 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -675,6 +675,7 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { ### Scala +暴力: ```scala object Solution { import scala.collection.mutable // 导包 @@ -701,5 +702,34 @@ object Solution { } ``` +剪枝: + +```scala +object Solution { + import scala.collection.mutable // 导包 + def combine(n: Int, k: Int): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() // 存放结果集 + var path = mutable.ListBuffer[Int]() //存放符合条件的结果 + + def backtracking(n: Int, k: Int, startIndex: Int): Unit = { + if (path.size == k) { + // 如果path的size == k就达到题目要求,添加到结果集,并返回 + result.append(path.toList) + return + } + // 剪枝优化 + for (i <- startIndex to (n - (k - path.size) + 1)) { + path.append(i) // 先把数字添加进去 + backtracking(n, k, i + 1) // 进行下一步回溯 + path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字 + } + } + + backtracking(n, k, 1) // 执行回溯 + result.toList // 最终返回result的List形式,return关键字可以省略 + } +} +``` + -----------------------
From 5ac414f466ac9d80511aa5c2c6659df8fa5419a0 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 2 Jun 2022 10:22:56 +0800 Subject: [PATCH 035/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合优化.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 94608ec1..2c1a821b 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -346,5 +346,34 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { } ``` +Scala: + +```scala +object Solution { + import scala.collection.mutable // 导包 + def combine(n: Int, k: Int): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() // 存放结果集 + var path = mutable.ListBuffer[Int]() //存放符合条件的结果 + + def backtracking(n: Int, k: Int, startIndex: Int): Unit = { + if (path.size == k) { + // 如果path的size == k就达到题目要求,添加到结果集,并返回 + result.append(path.toList) + return + } + // 剪枝优化 + for (i <- startIndex to (n - (k - path.size) + 1)) { + path.append(i) // 先把数字添加进去 + backtracking(n, k, i + 1) // 进行下一步回溯 + path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字 + } + } + + backtracking(n, k, 1) // 执行回溯 + result.toList // 最终返回result的List形式,return关键字可以省略 + } +} +``` + -----------------------
From d0814a723c8e293e2fa7eee1503114584269faa7 Mon Sep 17 00:00:00 2001 From: Mrchenuo Date: Thu, 2 Jun 2022 10:57:47 +0800 Subject: [PATCH 036/114] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复CPP代码的addAtIndex函数中,当index<0时,while会死循环的问题。 --- problems/0707.设计链表.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index dcdb53f4..a788939b 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -104,10 +104,14 @@ public: // 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。 // 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点 // 如果index大于链表的长度,则返回空 + // 如果index小于0,则置为0,作为链表的新头节点。 void addAtIndex(int index, int val) { if (index > _size) { return; } + else if (index < 0) { + index = 0; + } LinkedNode* newNode = new LinkedNode(val); LinkedNode* cur = _dummyHead; while(index--) { From 7dd463e085c8b7ef66e75009b3a5cdec0d30a17c Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 2 Jun 2022 12:13:52 +0800 Subject: [PATCH 037/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200216.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 32b1347e..9e7415be 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -511,5 +511,35 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { } ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def combinationSum3(k: Int, n: Int): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(k: Int, n: Int, sum: Int, startIndex: Int): Unit = { + if (sum > n) return // 剪枝,如果sum>目标和,就返回 + if (sum == n && path.size == k) { + result.append(path.toList) + return + } + // 剪枝 + for (i <- startIndex to (9 - (k - path.size) + 1)) { + path.append(i) + backtracking(k, n, sum + i, i + 1) + path = path.take(path.size - 1) + } + } + + backtracking(k, n, 0, 1) // 调用递归方法 + result.toList // 最终返回结果集的List形式 + } +} +``` + + -----------------------
From 2f0a1830377fe38fed12aba3bfd38424b9c67108 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 2 Jun 2022 16:28:28 +0800 Subject: [PATCH 038/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 94136565..f638349d 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -557,6 +557,37 @@ func letterCombinations(_ digits: String) -> [String] { } ``` +## Scala: + +```scala +object Solution { + import scala.collection.mutable + def letterCombinations(digits: String): List[String] = { + var result = mutable.ListBuffer[String]() + if(digits == "") return result.toList // 如果参数为空,返回空结果集的List形式 + var path = mutable.ListBuffer[Char]() + // 数字和字符的映射关系 + val map = Array[String]("", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz") + + def backtracking(index: Int): Unit = { + if (index == digits.size) { + result.append(path.mkString) // mkString语法:将数组类型直接转换为字符串 + return + } + var digit = digits(index) - '0' // 这里使用toInt会报错!必须 -'0' + for (i <- 0 until map(digit).size) { + path.append(map(digit)(i)) + backtracking(index + 1) + path = path.take(path.size - 1) + } + } + + backtracking(0) + result.toList + } +} +``` + -----------------------
From 3d83e14392b6f4c16ea3ea4bfaf175878dce27cf Mon Sep 17 00:00:00 2001 From: wenmimi Date: Thu, 2 Jun 2022 18:23:14 +0800 Subject: [PATCH 039/114] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1=20GO?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index a40a92ab..e24824b9 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -356,9 +356,13 @@ func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { // 递推公式 for i := 1; i < len(weight); i++ { //正序,也可以倒序 - for j := weight[i];j<= bagweight ; j++ { - dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) - } + for j := 0; j <= bagweight; j++ { + if j < weight[i] { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } + } } return dp[len(weight)-1][bagweight] } From d2e9927e97519621ba04f1e3d82684ed3cfafc5f Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 3 Jun 2022 09:46:09 +0800 Subject: [PATCH 040/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index e10a827f..ca6312f5 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -502,5 +502,35 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { } ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def combinationSum(candidates: Array[Int], target: Int): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(sum: Int, index: Int): Unit = { + if (sum == target) { + result.append(path.toList) // 如果正好等于target,就添加到结果集 + return + } + // 应该是从当前索引开始的,而不是从0 + // 剪枝优化:添加循环守卫,当sum + c(i) <= target的时候才循环,才可以进入下一次递归 + for (i <- index until candidates.size if sum + candidates(i) <= target) { + path.append(candidates(i)) + backtracking(sum + candidates(i), i) + path = path.take(path.size - 1) + } + } + + backtracking(0, 0) + result.toList + } +} +``` + + -----------------------
From 301a703ece704650eeb0bfd1eb8ef2079ee274b5 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 3 Jun 2022 15:24:28 +0800 Subject: [PATCH 041/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0040.组合总和II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 34ac64e6..6b635f8c 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -693,5 +693,37 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] { } ``` + +## Scala + +```scala +object Solution { + import scala.collection.mutable + def combinationSum2(candidates: Array[Int], target: Int): List[List[Int]] = { + var res = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + var candidate = candidates.sorted + + def backtracking(sum: Int, startIndex: Int): Unit = { + if (sum == target) { + res.append(path.toList) + return + } + + for (i <- startIndex until candidate.size if sum + candidate(i) <= target) { + if (!(i > startIndex && candidate(i) == candidate(i - 1))) { + path.append(candidate(i)) + backtracking(sum + candidate(i), i + 1) + path = path.take(path.size - 1) + } + } + } + + backtracking(0, 0) + res.toList + } +} +``` + -----------------------
From 3605046879d02c5f6b931f7440935ef4142ef121 Mon Sep 17 00:00:00 2001 From: yalexu <61576631+alexgy1@users.noreply.github.com> Date: Fri, 3 Jun 2022 18:24:51 +0800 Subject: [PATCH 042/114] Add JS correct reverse method --- problems/0344.反转字符串.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 58bada05..192a397c 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -190,13 +190,13 @@ javaScript: * @return {void} Do not return anything, modify s in-place instead. */ var reverseString = function(s) { - return s.reverse(); + //Do not return anything, modify s in-place instead. + reverse(s) }; -var reverseString = function(s) { +var reverse = function(s) { let l = -1, r = s.length; while(++l < --r) [s[l], s[r]] = [s[r], s[l]]; - return s; }; ``` From a2a2cf60272094c3c9d962723132502d93db097a Mon Sep 17 00:00:00 2001 From: lizhendong128 Date: Fri, 3 Jun 2022 21:42:36 +0800 Subject: [PATCH 043/114] =?UTF-8?q?=E4=BF=AE=E6=94=B90106=E4=BB=8E?= =?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将0106和0105的Java版本进行了修改,采用了map来存储位置信息,加快定位;并且代码更容易看懂 --- .../0106.从中序与后序遍历序列构造二叉树.md | 86 +++++++++---------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 188ad3cb..878c3572 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -584,35 +584,29 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 ```java class Solution { + Map map; // 方便根据数值查找位置 public TreeNode buildTree(int[] inorder, int[] postorder) { - return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length); + map = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置 + map.put(inorder[i], i); + } + + return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开 } - public TreeNode buildTree1(int[] inorder, int inLeft, int inRight, - int[] postorder, int postLeft, int postRight) { - // 没有元素了 - if (inRight - inLeft < 1) { + + public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) { + // 参数里的范围都是前闭后开 + if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树 return null; } - // 只有一个元素了 - if (inRight - inLeft == 1) { - return new TreeNode(inorder[inLeft]); - } - // 后序数组postorder里最后一个即为根结点 - int rootVal = postorder[postRight - 1]; - TreeNode root = new TreeNode(rootVal); - int rootIndex = 0; - // 根据根结点的值找到该值在中序数组inorder里的位置 - for (int i = inLeft; i < inRight; i++) { - if (inorder[i] == rootVal) { - rootIndex = i; - break; - } - } - // 根据rootIndex划分左右子树 - root.left = buildTree1(inorder, inLeft, rootIndex, - postorder, postLeft, postLeft + (rootIndex - inLeft)); - root.right = buildTree1(inorder, rootIndex + 1, inRight, - postorder, postLeft + (rootIndex - inLeft), postRight - 1); + int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置 + TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 + int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数 + root.left = findNode(inorder, inBegin, rootIndex, + postorder, postBegin, postBegin + lenOfLeft); + root.right = findNode(inorder, rootIndex + 1, inEnd, + postorder, postBegin + lenOfLeft, postEnd - 1); + return root; } } @@ -622,31 +616,29 @@ class Solution { ```java class Solution { + Map map; public TreeNode buildTree(int[] preorder, int[] inorder) { - return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); - } - - public TreeNode helper(int[] preorder, int preLeft, int preRight, - int[] inorder, int inLeft, int inRight) { - // 递归终止条件 - if (inLeft > inRight || preLeft > preRight) return null; - - // val 为前序遍历第一个的值,也即是根节点的值 - // idx 为根据根节点的值来找中序遍历的下标 - int idx = inLeft, val = preorder[preLeft]; - TreeNode root = new TreeNode(val); - for (int i = inLeft; i <= inRight; i++) { - if (inorder[i] == val) { - idx = i; - break; - } + map = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置 + map.put(inorder[i], i); } - // 根据 idx 来递归找左右子树 - root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft), - inorder, inLeft, idx - 1); - root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight, - inorder, idx + 1, inRight); + return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开 + } + + public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) { + // 参数里的范围都是前闭后开 + if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树 + return null; + } + int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置 + TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 + int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数 + root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1, + inorder, inBegin, rootIndex); + root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd, + inorder, rootIndex + 1, inEnd); + return root; } } From af1b3e3e32676d6037645c0553b4fc2bb2cf149d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?= <20304773@qq.com> Date: Sat, 4 Jun 2022 00:22:14 +0800 Subject: [PATCH 044/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880209.?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index fbef7692..ceb06ce9 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -448,6 +448,27 @@ object Solution { } } ``` - +C#: +```csharp +public class Solution { + public int MinSubArrayLen(int s, int[] nums) { + int n = nums.Length; + int ans = int.MaxValue; + int start = 0, end = 0; + int sum = 0; + while (end < n) { + sum += nums[end]; + while (sum >= s) + { + ans = Math.Min(ans, end - start + 1); + sum -= nums[start]; + start++; + } + end++; + } + return ans == int.MaxValue ? 0 : ans; + } +} +``` -----------------------
From cccb4973aeccde06a00e5c693510c824ae98c99b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 10:44:25 +0800 Subject: [PATCH 045/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880035.?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0035.搜索插入位置.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 6c04e7de..3de7c4f7 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -283,6 +283,28 @@ var searchInsert = function (nums, target) { }; ``` +### TypeScript + +```typescript +// 第一种二分法 +function searchInsert(nums: number[], target: number): number { + const length: number = nums.length; + let left: number = 0, + right: number = length - 1; + while (left <= right) { + const mid: number = Math.floor((left + right) / 2); + if (nums[mid] < target) { + left = mid + 1; + } else if (nums[mid] === target) { + return mid; + } else { + right = mid - 1; + } + } + return right + 1; +}; +``` + ### Swift ```swift From 6cd58dfef0a22f568bb5c6b03f4026520b7283de Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 13:13:33 +0800 Subject: [PATCH 046/114] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880024.?= =?UTF-8?q?=E4=B8=A4=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E4=BC=98?= =?UTF-8?q?=E5=8C=96typescript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=98=93=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0024.两两交换链表中的节点.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 2289c229..0d848e4d 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -254,20 +254,19 @@ TypeScript: ```typescript function swapPairs(head: ListNode | null): ListNode | null { - 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; -} + const dummyNode: ListNode = new ListNode(0, head); + let curNode: ListNode | null = dummyNode; + while (curNode && curNode.next && curNode.next.next) { + let firstNode: ListNode = curNode.next, + secNode: ListNode = curNode.next.next, + thirdNode: ListNode | null = curNode.next.next.next; + curNode.next = secNode; + secNode.next = firstNode; + firstNode.next = thirdNode; + curNode = firstNode; + } + return dummyNode.next; +}; ``` Kotlin: From 7d5856d0b82baad2266555f33bc1eae43538fd70 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 15:56:22 +0800 Subject: [PATCH 047/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880234.?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0234.回文链表.md | 59 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md index db910d4e..b19a2408 100644 --- a/problems/0234.回文链表.md +++ b/problems/0234.回文链表.md @@ -273,7 +273,7 @@ class Solution: return pre ``` -## Go +### Go ```go @@ -319,6 +319,63 @@ var isPalindrome = function(head) { }; ``` +### TypeScript + +> 数组模拟 + +```typescript +function isPalindrome(head: ListNode | null): boolean { + const helperArr: number[] = []; + let curNode: ListNode | null = head; + while (curNode !== null) { + helperArr.push(curNode.val); + curNode = curNode.next; + } + let left: number = 0, + right: number = helperArr.length - 1; + while (left < right) { + if (helperArr[left++] !== helperArr[right--]) return false; + } + return true; +}; +``` + +> 反转后半部分链表 + +```typescript +function isPalindrome(head: ListNode | null): boolean { + if (head === null || head.next === null) return true; + let fastNode: ListNode | null = head, + slowNode: ListNode = head, + preNode: ListNode = head; + while (fastNode !== null && fastNode.next !== null) { + preNode = slowNode; + slowNode = slowNode.next!; + fastNode = fastNode.next.next; + } + preNode.next = null; + let cur1: ListNode | null = head; + let cur2: ListNode | null = reverseList(slowNode); + while (cur1 !== null) { + if (cur1.val !== cur2!.val) return false; + cur1 = cur1.next; + cur2 = cur2!.next; + } + return true; +}; +function reverseList(head: ListNode | null): ListNode | null { + let curNode: ListNode | null = head, + preNode: ListNode | null = null; + while (curNode !== null) { + let tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +} +``` + ----------------------- From bb484a70ac2c8d2fcbbba08a1897bc053c9354d1 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 21:01:59 +0800 Subject: [PATCH 048/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.?= =?UTF-8?q?=E9=87=8D=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0143.重排链表.md | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 790bcb48..c60fc0f9 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -6,6 +6,8 @@ # 143.重排链表 +[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/) + ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png) ## 思路 @@ -465,7 +467,81 @@ var reorderList = function(head, s = [], tmp) { } ``` +### TypeScript + +> 辅助数组法: + +```typescript +function reorderList(head: ListNode | null): void { + if (head === null) return; + const helperArr: ListNode[] = []; + let curNode: ListNode | null = head; + while (curNode !== null) { + helperArr.push(curNode); + curNode = curNode.next; + } + let node: ListNode = head; + let left: number = 1, + right: number = helperArr.length - 1; + let count: number = 0; + while (left <= right) { + if (count % 2 === 0) { + node.next = helperArr[right--]; + } else { + node.next = helperArr[left++]; + } + count++; + node = node.next; + } + node.next = null; +}; +``` + +> 分割链表法: + +```typescript +function reorderList(head: ListNode | null): void { + if (head === null || head.next === null) return; + let fastNode: ListNode = head, + slowNode: ListNode = head; + while (fastNode.next !== null && fastNode.next.next !== null) { + slowNode = slowNode.next!; + fastNode = fastNode.next.next; + } + let head1: ListNode | null = head; + // 反转后半部分链表 + let head2: ListNode | null = reverseList(slowNode.next); + // 分割链表 + slowNode.next = null; + /** + 直接在head1链表上进行插入 + head1 链表长度一定大于或等于head2, + 因此在下面的循环中,只要head2不为null, head1 一定不为null + */ + while (head2 !== null) { + const tempNode1: ListNode | null = head1!.next, + tempNode2: ListNode | null = head2.next; + head1!.next = head2; + head2.next = tempNode1; + head1 = tempNode1; + head2 = tempNode2; + } +}; +function reverseList(head: ListNode | null): ListNode | null { + let curNode: ListNode | null = head, + preNode: ListNode | null = null; + while (curNode !== null) { + const tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +} +``` + ### C + 方法三:反转链表 ```c //翻转链表 From c0a73e2544202baa3839852777e053e82937004c Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 4 Jun 2022 21:35:24 +0800 Subject: [PATCH 049/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 7a702898..f361d1ef 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -676,5 +676,50 @@ impl Solution { } } ``` + + +## Scala + +```scala +object Solution { + + import scala.collection.mutable + + def partition(s: String): List[List[String]] = { + var result = mutable.ListBuffer[List[String]]() + var path = mutable.ListBuffer[String]() + + // 判断字符串是否回文 + def isPalindrome(start: Int, end: Int): Boolean = { + var (left, right) = (start, end) + while (left < right) { + if (s(left) != s(right)) return false + left += 1 + right -= 1 + } + true + } + + // 回溯算法 + def backtracking(startIndex: Int): Unit = { + if (startIndex >= s.size) { + result.append(path.toList) + return + } + // 添加循环守卫,如果当前分割是回文子串则进入回溯 + for (i <- startIndex until s.size if isPalindrome(startIndex, i)) { + path.append(s.substring(startIndex, i + 1)) + backtracking(i + 1) + path = path.take(path.size - 1) + } + } + + backtracking(0) + result.toList + } +} +``` + + -----------------------
From a8e4d4da767758488dbbc20564eab01a08e3e2dc Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 4 Jun 2022 22:04:15 +0800 Subject: [PATCH 050/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 6401824b..48bb6df2 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -659,6 +659,48 @@ func restoreIpAddresses(_ s: String) -> [String] { } ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def restoreIpAddresses(s: String): List[String] = { + var result = mutable.ListBuffer[String]() + if (s.size < 4 || s.length > 12) return result.toList + var path = mutable.ListBuffer[String]() + + // 判断IP中的一个字段是否为正确的 + def isIP(sub: String): Boolean = { + if (sub.size > 1 && sub(0) == '0') return false + if (sub.toInt > 255) return false + true + } + + def backtracking(startIndex: Int): Unit = { + if (startIndex >= s.size) { + if (path.size == 4) { + result.append(path.mkString(".")) // mkString方法可以把集合里的数据以指定字符串拼接 + return + } + return + } + // subString + for (i <- startIndex until startIndex + 3 if i < s.size) { + var subString = s.substring(startIndex, i + 1) + if (isIP(subString)) { // 如果合法则进行下一轮 + path.append(subString) + backtracking(i + 1) + path = path.take(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + -----------------------
From f5268ee213dafe8e7ab1c28d722a6b7dc81f4029 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 22:26:58 +0800 Subject: [PATCH 051/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880141.?= =?UTF-8?q?=E7=8E=AF=E5=BD=A2=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0141.环形链表.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index ddd83c94..ce90b6c4 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -7,6 +7,8 @@ # 141. 环形链表 +[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/) + 给定一个链表,判断链表中是否有环。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。 @@ -103,7 +105,7 @@ class Solution: return False ``` -## Go +### Go ```go func hasCycle(head *ListNode) bool { @@ -139,6 +141,23 @@ var hasCycle = function(head) { }; ``` +### TypeScript + +```typescript +function hasCycle(head: ListNode | null): boolean { + let slowNode: ListNode | null = head, + fastNode: ListNode | null = head; + while (fastNode !== null && fastNode.next !== null) { + slowNode = slowNode!.next; + fastNode = fastNode.next.next; + if (slowNode === fastNode) return true; + } + return false; +}; +``` + + + -----------------------
From 0245b3ae50cc612ac7765976171b670a954f6a34 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 4 Jun 2022 23:38:12 +0800 Subject: [PATCH 052/114] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880142.?= =?UTF-8?q?=E7=8E=AF=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E4=BC=98=E5=8C=96typescript=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index f8e62d45..7d3c8443 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -301,13 +301,13 @@ function detectCycle(head: ListNode | null): ListNode | null { let slowNode: ListNode | null = head, fastNode: ListNode | null = head; while (fastNode !== null && fastNode.next !== null) { - slowNode = (slowNode as ListNode).next; + slowNode = slowNode!.next; fastNode = fastNode.next.next; if (slowNode === fastNode) { slowNode = head; while (slowNode !== fastNode) { - slowNode = (slowNode as ListNode).next; - fastNode = (fastNode as ListNode).next; + slowNode = slowNode!.next; + fastNode = fastNode!.next; } return slowNode; } From bc3d202797ee1a2c276efd2b0b86cc00bd0f744a Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 5 Jun 2022 10:54:31 +0800 Subject: [PATCH 053/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880205.?= =?UTF-8?q?=E5=90=8C=E6=9E=84=E5=AD=97=E7=AC=A6=E4=B8=B2.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0205.同构字符串.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index d4b71c59..337dcc73 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -156,6 +156,28 @@ var isIsomorphic = function(s, t) { }; ``` +## TypeScript + +```typescript +function isIsomorphic(s: string, t: string): boolean { + const helperMap1: Map = new Map(); + const helperMap2: Map = new Map(); + for (let i = 0, length = s.length; i < length; i++) { + let temp1: string | undefined = helperMap1.get(s[i]); + let temp2: string | undefined = helperMap2.get(t[i]); + if (temp1 === undefined && temp2 === undefined) { + helperMap1.set(s[i], t[i]); + helperMap2.set(t[i], s[i]); + } else if (temp1 !== t[i] || temp2 !== s[i]) { + return false; + } + } + return true; +}; +``` + + + -----------------------
From ccfb805417aa1f55efd1b30be662db8a92b46893 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 5 Jun 2022 13:50:27 +0800 Subject: [PATCH 054/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index e1c52b5b..3fc396a2 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -373,6 +373,60 @@ func subsets(_ nums: [Int]) -> [[Int]] { } ``` +## Scala + +思路一: 使用本题解思路 + +```scala +object Solution { + import scala.collection.mutable + def subsets(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(startIndex: Int): Unit = { + result.append(path.toList) // 存放结果 + if (startIndex >= nums.size) { + return + } + for (i <- startIndex until nums.size) { + path.append(nums(i)) // 添加元素 + backtracking(i + 1) + path.remove(path.size - 1) // 删除 + } + } + + backtracking(0) + result.toList + } +} +``` + +思路二: 将原问题转换为二叉树,针对每一个元素都有**选或不选**两种选择,直到遍历到最后,所有的叶子节点即为本题的答案: + +```scala +object Solution { + import scala.collection.mutable + def subsets(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + + def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = { + if (startIndex == nums.length) { + result.append(path.toList) + return + } + path.append(nums(startIndex)) + backtracking(path, startIndex + 1) // 选择元素 + path.remove(path.size - 1) + backtracking(path, startIndex + 1) // 不选择元素 + } + + backtracking(mutable.ListBuffer[Int](), 0) + result.toList + } +} +``` + -----------------------
From b3530189489235f1212b89bd9dba0281154f366c Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 5 Jun 2022 14:13:41 +0800 Subject: [PATCH 055/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 74ce000b..9047a809 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -434,6 +434,63 @@ func subsetsWithDup(_ nums: [Int]) -> [[Int]] { } ``` +### Scala + +不使用userd数组: + +```scala +object Solution { + import scala.collection.mutable + def subsetsWithDup(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + var num = nums.sorted // 排序 + + def backtracking(startIndex: Int): Unit = { + result.append(path.toList) + if (startIndex >= num.size){ + return + } + for (i <- startIndex until num.size) { + // 同一树层重复的元素不进入回溯 + if (!(i > startIndex && num(i) == num(i - 1))) { + path.append(num(i)) + backtracking(i + 1) + path.remove(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + +使用Set去重: +```scala +object Solution { + import scala.collection.mutable + def subsetsWithDup(nums: Array[Int]): List[List[Int]] = { + var result = mutable.Set[List[Int]]() + var num = nums.sorted + def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = { + if (startIndex == num.length) { + result.add(path.toList) + return + } + path.append(num(startIndex)) + backtracking(path, startIndex + 1) // 选择 + path.remove(path.size - 1) + backtracking(path, startIndex + 1) // 不选择 + } + + backtracking(mutable.ListBuffer[Int](), 0) + + result.toList + } +} +``` -----------------------
From 068cc095353ceb240c98a6c2b8598e98c445260b Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sun, 5 Jun 2022 14:46:21 +0800 Subject: [PATCH 056/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 3ea2382b..a04d433b 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -522,5 +522,39 @@ func findSubsequences(_ nums: [Int]) -> [[Int]] { ``` +## Scala + +```scala +object Solution { + import scala.collection.mutable + def findSubsequences(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(startIndex: Int): Unit = { + // 集合元素大于1,添加到结果集 + if (path.size > 1) { + result.append(path.toList) + } + + var used = new Array[Boolean](201) + // 使用循环守卫,当前层没有用过的元素才有资格进入回溯 + for (i <- startIndex until nums.size if !used(nums(i) + 100)) { + // 如果path没元素或 当前循环的元素比path的最后一个元素大,则可以进入回溯 + if (path.size == 0 || (!path.isEmpty && nums(i) >= path(path.size - 1))) { + used(nums(i) + 100) = true + path.append(nums(i)) + backtracking(i + 1) + path.remove(path.size - 1) + } + } + } + + backtracking(0) + result.toList + } +} +``` + -----------------------
From 9f38af3e3a61562b17530302e8d14a6a02d1835f Mon Sep 17 00:00:00 2001 From: Hang <69448559+silaslll@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:59:15 -0400 Subject: [PATCH 057/114] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?= =?UTF-8?q?=E6=B0=94=E7=90=83.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新了java代码,增加了一个 leftmostRightBound variable 记录最小的右边界使得代码可读性增加 加入了comment 解释了Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0])); 中不用 x[0] - y[0] 而是用Integer.compare(x[0], y[0]) 的原因 加入了时空复杂度和说明 --- problems/0452.用最少数量的箭引爆气球.md | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index d4bbe961..58422d4c 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -136,17 +136,28 @@ public: ### Java ```java +/** +时间复杂度 : O(NlogN) 排序需要 O(NlogN) 的复杂度 + +空间复杂度 : O(logN) java所使用的内置函数用的是快速排序需要 logN 的空间 +*/ class Solution { public int findMinArrowShots(int[][] points) { if (points.length == 0) return 0; - Arrays.sort(points, (o1, o2) -> Integer.compare(o1[0], o2[0])); - + //用x[0] - y[0] 会大于2147483647 造成整型溢出 + Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0])); + //count = 1 因为最少需要一个箭来射击第一个气球 int count = 1; - for (int i = 1; i < points.length; i++) { - if (points[i][0] > points[i - 1][1]) { + //重叠气球的最小右边界 + int leftmostRightBound = points[0][1]; + //如果下一个气球的左边界大于最小右边界 + if (points[i][0] > leftmostRightBound ) { + //增加一次射击 count++; + leftmostRightBound = points[i][1]; + //不然就更新最小右边界 } else { - points[i][1] = Math.min(points[i][1],points[i - 1][1]); + leftmostRightBound = Math.min(leftmostRightBound , points[i][1]); } } return count; From 6d804e2b244891ab3c1a12ac9b34d3eafd64654c Mon Sep 17 00:00:00 2001 From: Grant Yang Date: Sun, 5 Jun 2022 21:24:56 -0400 Subject: [PATCH 058/114] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF=E5=8F=8A?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 1743243d..d9fd0b30 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2393,21 +2393,21 @@ JavaScript: var minDepth = function(root) { if (root === null) return 0; let queue = [root]; - let deepth = 0; + let depth = 0; while (queue.length) { let n = queue.length; - deepth++; + depth++; for (let i=0; i Date: Sun, 5 Jun 2022 22:31:41 -0400 Subject: [PATCH 059/114] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index e444a221..98848963 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -136,24 +136,38 @@ public: ### Java ```java + +/** +时间复杂度 : O(NlogN) 排序需要O(NlogN) +空间复杂度 : O(logN) java 的内置排序是快速排序 需要 O(logN)空间 + +*/ class Solution { public int[][] merge(int[][] intervals) { List res = new LinkedList<>(); - Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0])); - + //按照左边界排序 + Arrays.sort(intervals, (x, y) -> Integer.compare(x[0], y[0])); + //initial start 是最小左边界 int start = intervals[0][0]; + int rightmostRightBound = intervals[0][1]; for (int i = 1; i < intervals.length; i++) { - if (intervals[i][0] > intervals[i - 1][1]) { - res.add(new int[]{start, intervals[i - 1][1]}); + //如果左边界大于最大右边界 + if (intervals[i][0] > rightmostRightBound) { + //加入区间 并且更新start + res.add(new int[]{start, rightmostRightBound}); start = intervals[i][0]; + rightmostRightBound = intervals[i][1]; } else { - intervals[i][1] = Math.max(intervals[i][1], intervals[i - 1][1]); + //更新最大右边界 + rightmostRightBound = Math.max(rightmostRightBound, intervals[i][1]); } } - res.add(new int[]{start, intervals[intervals.length - 1][1]}); + res.add(new int[]{start, rightmostRightBound}); return res.toArray(new int[res.size()][]); } } + +} ``` ```java // 版本2 From 091204c926a044f0ec86200cbdd3cca6deeaf97e Mon Sep 17 00:00:00 2001 From: HanMengnan <1448189829@qq.com> Date: Tue, 7 Jun 2022 10:46:43 +0800 Subject: [PATCH 060/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880129.?= =?UTF-8?q?=E6=B1=82=E6=A0=B9=E8=8A=82=E7=82=B9=E5=88=B0=E5=8F=B6=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0129.求根到叶子节点数字之和.md | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index b271ca7d..4191bb26 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -3,6 +3,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + + # 129. 求根节点到叶节点数字之和 [力扣题目链接](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/) @@ -245,6 +248,29 @@ class Solution: ``` Go: +```go +func sumNumbers(root *TreeNode) int { + sum = 0 + travel(root, root.Val) + return sum +} + +func travel(root *TreeNode, tmpSum int) { + if root.Left == nil && root.Right == nil { + sum += tmpSum + } else { + if root.Left != nil { + travel(root.Left, tmpSum*10+root.Left.Val) + } + if root.Right != nil { + travel(root.Right, tmpSum*10+root.Right.Val) + } + } +} +``` + + + JavaScript: ```javascript var sumNumbers = function(root) { From 97fc88e533418bf9070bd9fb549a23a2499805b4 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 7 Jun 2022 16:59:55 +0800 Subject: [PATCH 061/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200046.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index 836c3646..ec3adaa7 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -456,6 +456,36 @@ func permute(_ nums: [Int]) -> [[Int]] { } ``` +### Scala + +```scala +object Solution { + import scala.collection.mutable + def permute(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + + def backtracking(used: Array[Boolean]): Unit = { + if (path.size == nums.size) { + // 如果path的长度和nums相等,那么可以添加到结果集 + result.append(path.toList) + return + } + // 添加循环守卫,只有当当前数字没有用过的情况下才进入回溯 + for (i <- nums.indices if used(i) == false) { + used(i) = true + path.append(nums(i)) + backtracking(used) // 回溯 + path.remove(path.size - 1) + used(i) = false + } + } + + backtracking(new Array[Boolean](nums.size)) // 调用方法 + result.toList // 最终返回结果集的List形式 + } +} +``` -----------------------
From 8537401616f10ce85038bbd94aa951a20bb877d6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 7 Jun 2022 17:24:41 +0800 Subject: [PATCH 062/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index cce25cd9..0a5debcc 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -422,5 +422,43 @@ int** permuteUnique(int* nums, int numsSize, int* returnSize, int** returnColumn } ``` +### Scala + +```scala +object Solution { + import scala.collection.mutable + def permuteUnique(nums: Array[Int]): List[List[Int]] = { + var result = mutable.ListBuffer[List[Int]]() + var path = mutable.ListBuffer[Int]() + var num = nums.sorted // 首先对数据进行排序 + + def backtracking(used: Array[Boolean]): Unit = { + if (path.size == num.size) { + // 如果path的size等于num了,那么可以添加到结果集 + result.append(path.toList) + return + } + // 循环守卫,当前元素没被使用过就进入循环体 + for (i <- num.indices if used(i) == false) { + // 当前索引为0,不存在和前一个数字相等可以进入回溯 + // 当前索引值和上一个索引不相等,可以回溯 + // 前一个索引对应的值没有被选,可以回溯 + // 因为Scala没有continue,只能将逻辑反过来写 + if (i == 0 || (i > 0 && num(i) != num(i - 1)) || used(i-1) == false) { + used(i) = true + path.append(num(i)) + backtracking(used) + path.remove(path.size - 1) + used(i) = false + } + } + } + + backtracking(new Array[Boolean](nums.length)) + result.toList + } +} +``` + -----------------------
From fa26fb332b43cbeec805944f4522472953232487 Mon Sep 17 00:00:00 2001 From: dcj_hp <294487055@qq.com> Date: Tue, 7 Jun 2022 17:38:37 +0800 Subject: [PATCH 063/114] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20java=20dp=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0132.分割回文串II.md | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md index 87d3e4b4..4cb95901 100644 --- a/problems/0132.分割回文串II.md +++ b/problems/0132.分割回文串II.md @@ -206,6 +206,55 @@ public: ## Java ```java +class Solution { + + public int minCut(String s) { + if(null == s || "".equals(s)){ + return 0; + } + int len = s.length(); + // 1. + // 记录子串[i..j]是否是回文串 + boolean[][] isPalindromic = new boolean[len][len]; + // 从下到上,从左到右 + for(int i = len - 1; i >= 0; i--){ + for(int j = i; j < len; j++){ + if(s.charAt(i) == s.charAt(j)){ + if(j - i <= 1){ + isPalindromic[i][j] = true; + } else{ + isPalindromic[i][j] = isPalindromic[i + 1][j - 1]; + } + } else{ + isPalindromic[i][j] = false; + } + } + } + + // 2. + // dp[i] 表示[0..i]的最小分割次数 + int[] dp = new int[len]; + for(int i = 0; i < len; i++){ + //初始考虑最坏的情况。 1个字符分割0次, len个字符分割 len - 1次 + dp[i] = i; + } + + for(int i = 1; i < len; i++){ + if(isPalindromic[0][i]){ + // s[0..i]是回文了,那 dp[i] = 0, 一次也不用分割 + dp[i] = 0; + continue; + } + for(int j = 0; j < i; j++){ + // 按文中的思路,不清楚就拿 "ababa" 为例,先写出 isPalindromic 数组,再进行求解 + if(isPalindromic[j + 1][i]){ + dp[i] = Math.min(dp[i], dp[j] + 1); + } + } + } + return dp[len - 1]; + } +} ``` ## Python @@ -240,6 +289,7 @@ class Solution: ## Go ```go + ``` ## JavaScript From 7b95f173683f4eae7fd68c313839a678b2e1b79d Mon Sep 17 00:00:00 2001 From: Chris Chen Date: Tue, 7 Jun 2022 13:21:32 +0100 Subject: [PATCH 064/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9ADart=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 6969c2e2..4ba92092 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -317,6 +317,20 @@ public class Solution { } ``` +Dart: +```dart +List twoSum(List nums, int target) { + var tmp = []; + for (var i = 0; i < nums.length; i++) { + var rest = target - nums[i]; + if(tmp.contains(rest)){ + return [tmp.indexOf(rest), i]; + } + tmp.add(nums[i]); + } + return [0 , 0]; +} +``` -----------------------
From 371564ba3b9a2b9641cafc05eb033f8d162aec81 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 7 Jun 2022 22:19:13 +0800 Subject: [PATCH 065/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8E.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 74 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index c03e48c2..f65ccaf5 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -455,7 +455,7 @@ var solveNQueens = function(n) { }; ``` -## TypeScript +### TypeScript ```typescript function solveNQueens(n: number): string[][] { @@ -683,5 +683,77 @@ char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes){ } ``` +### Scala + +```scala +object Solution { + import scala.collection.mutable + def solveNQueens(n: Int): List[List[String]] = { + var result = mutable.ListBuffer[List[String]]() + + def judge(x: Int, y: Int, maze: Array[Array[Boolean]]): Boolean = { + // 正上方 + var xx = x + while (xx >= 0) { + if (maze(xx)(y)) return false + xx -= 1 + } + // 左边 + var yy = y + while (yy >= 0) { + if (maze(x)(yy)) return false + yy -= 1 + } + // 左上方 + xx = x + yy = y + while (xx >= 0 && yy >= 0) { + if (maze(xx)(yy)) return false + xx -= 1 + yy -= 1 + } + xx = x + yy = y + // 右上方 + while (xx >= 0 && yy < n) { + if (maze(xx)(yy)) return false + xx -= 1 + yy += 1 + } + true + } + + def backtracking(row: Int, maze: Array[Array[Boolean]]): Unit = { + if (row == n) { + // 将结果转换为题目所需要的形式 + var path = mutable.ListBuffer[String]() + for (x <- maze) { + var tmp = mutable.ListBuffer[String]() + for (y <- x) { + if (y == true) tmp.append("Q") + else tmp.append(".") + } + path.append(tmp.mkString) + } + result.append(path.toList) + return + } + + for (j <- 0 until n) { + // 判断这个位置是否可以放置皇后 + if (judge(row, j, maze)) { + maze(row)(j) = true + backtracking(row + 1, maze) + maze(row)(j) = false + } + } + } + + backtracking(0, Array.ofDim[Boolean](n, n)) + result.toList + } +} +``` + -----------------------
From 237dc9d6b3c648d4a76e905bbc7059c2f573e91a Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 8 Jun 2022 12:52:55 +0800 Subject: [PATCH 066/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880925.?= =?UTF-8?q?=E9=95=BF=E6=8C=89=E9=94=AE=E5=85=A5.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0925.长按键入.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0925.长按键入.md b/problems/0925.长按键入.md index 0ef5a3d7..feb57391 100644 --- a/problems/0925.长按键入.md +++ b/problems/0925.长按键入.md @@ -209,6 +209,31 @@ var isLongPressedName = function(name, typed) { }; ``` +### TypeScript + +```typescript +function isLongPressedName(name: string, typed: string): boolean { + const nameLength: number = name.length, + typeLength: number = typed.length; + let i: number = 0, + j: number = 0; + while (i < nameLength && j < typeLength) { + if (name[i] !== typed[j]) return false; + i++; + j++; + if (i === nameLength || name[i] !== name[i - 1]) { + // 跳过typed中的连续相同字符 + while (j < typeLength && typed[j] === typed[j - 1]) { + j++; + } + } + } + return i === nameLength && j === typeLength; +}; +``` + + + -----------------------
From cded6c5c803e5d6de7a47e020e94ce03fcc68432 Mon Sep 17 00:00:00 2001 From: ExplosiveBattery <641370196@qq.com> Date: Thu, 9 Jun 2022 00:58:38 +0800 Subject: [PATCH 067/114] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20=20python=20code=20via=20itera?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the original method, we need to traversal every node and write the function named getDepth to get the depth of all sub trees in traverse method too. But there is more suitable uniform iteration traversal algorithm, I use the map struct in the code segment where the node is Null. If you have problem in understand, please feel free to communicate with me. --- problems/0110.平衡二叉树.md | 46 +++++++++++++------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index d98ff8a9..1b997643 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -531,40 +531,26 @@ class Solution: 迭代法: ```python class Solution: - def isBalanced(self, root: TreeNode) -> bool: - st = [] + def isBalanced(self, root: Optional[TreeNode]) -> bool: if not root: return True - st.append(root) - while st: - node = st.pop() #中 - if abs(self.getDepth(node.left) - self.getDepth(node.right)) > 1: - return False - if node.right: - st.append(node.right) #右(空节点不入栈) - if node.left: - st.append(node.left) #左(空节点不入栈) - return True - - def getDepth(self, cur): - st = [] - if cur: - st.append(cur) - depth = 0 - result = 0 - while st: - node = st.pop() + + height_map = {} + stack = [root] + while stack: + node = stack.pop() if node: - st.append(node) #中 - st.append(None) - depth += 1 - if node.right: st.append(node.right) #右 - if node.left: st.append(node.left) #左 + stack.append(node) + stack.append(None) + if node.left: stack.append(node.left) + if node.right: stack.append(node.right) else: - node = st.pop() - depth -= 1 - result = max(result, depth) - return result + real_node = stack.pop() + left, right = height_map.get(real_node.left, 0), height_map.get(real_node.right, 0) + if abs(left - right) > 1: + return False + height_map[real_node] = 1 + max(left, right) + return True ``` From e354cd6e25231fc9b255ae1d6ed3d1c58e4aa27d Mon Sep 17 00:00:00 2001 From: ExplosiveBattery <641370196@qq.com> Date: Thu, 9 Jun 2022 02:33:55 +0800 Subject: [PATCH 068/114] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20level=20order=20traversal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This leetcode problem can use level order traversal method, the difference between with the normal version is we should judge for None. There is my python answer, please feel free to contact with me if you have any problem. --- problems/0101.对称二叉树.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 1eb43589..caf5d249 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -437,6 +437,31 @@ class Solution: return True ``` +层次遍历 +```python +class Solution: + def isSymmetric(self, root: Optional[TreeNode]) -> bool: + if not root: + return True + + que = [root] + while que: + this_level_length = len(que) + for i in range(this_level_length // 2): + # 要么其中一个是None但另外一个不是 + if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]): + return False + # 要么两个都不是None + if que[i] and que[i].val != que[this_level_length - 1 - i].val: + return False + for i in range(this_level_length): + if not que[i]: continue + que.append(que[i].left) + que.append(que[i].right) + que = que[this_level_length:] + return True +``` + ## Go ```go From e06b53a88d8b26b4840428255b0e1f65f0eaf938 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 9 Jun 2022 21:57:15 +0800 Subject: [PATCH 069/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200037.=E8=A7=A3?= =?UTF-8?q?=E6=95=B0=E7=8B=AC.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0037.解数独.md | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md index c1ac15af..e2c533a9 100644 --- a/problems/0037.解数独.md +++ b/problems/0037.解数独.md @@ -602,5 +602,100 @@ func solveSudoku(_ board: inout [[Character]]) { } ``` +### Scala + +详细写法: +```scala +object Solution { + + def solveSudoku(board: Array[Array[Char]]): Unit = { + backtracking(board) + } + + def backtracking(board: Array[Array[Char]]): Boolean = { + for (i <- 0 until 9) { + for (j <- 0 until 9) { + if (board(i)(j) == '.') { // 必须是为 . 的数字才放数字 + for (k <- '1' to '9') { // 这个位置放k是否合适 + if (isVaild(i, j, k, board)) { + board(i)(j) = k + if (backtracking(board)) return true // 找到了立刻返回 + board(i)(j) = '.' // 回溯 + } + } + return false // 9个数都试完了,都不行就返回false + } + } + } + true // 遍历完所有的都没返回false,说明找到了 + } + + def isVaild(x: Int, y: Int, value: Char, board: Array[Array[Char]]): Boolean = { + // 行 + for (i <- 0 until 9 ) { + if (board(i)(y) == value) { + return false + } + } + + // 列 + for (j <- 0 until 9) { + if (board(x)(j) == value) { + return false + } + } + + // 宫 + var row = (x / 3) * 3 + var col = (y / 3) * 3 + for (i <- row until row + 3) { + for (j <- col until col + 3) { + if (board(i)(j) == value) { + return false + } + } + } + + true + } +} +``` + +遵循Scala至简原则写法: +```scala +object Solution { + + def solveSudoku(board: Array[Array[Char]]): Unit = { + backtracking(board) + } + + def backtracking(board: Array[Array[Char]]): Boolean = { + // 双重for循环 + 循环守卫 + for (i <- 0 until 9; j <- 0 until 9 if board(i)(j) == '.') { + // 必须是为 . 的数字才放数字,使用循环守卫判断该位置是否可以放置当前循环的数字 + for (k <- '1' to '9' if isVaild(i, j, k, board)) { // 这个位置放k是否合适 + board(i)(j) = k + if (backtracking(board)) return true // 找到了立刻返回 + board(i)(j) = '.' // 回溯 + } + return false // 9个数都试完了,都不行就返回false + } + true // 遍历完所有的都没返回false,说明找到了 + } + + def isVaild(x: Int, y: Int, value: Char, board: Array[Array[Char]]): Boolean = { + // 行,循环守卫进行判断 + for (i <- 0 until 9 if board(i)(y) == value) return false + // 列,循环守卫进行判断 + for (j <- 0 until 9 if board(x)(j) == value) return false + // 宫,循环守卫进行判断 + var row = (x / 3) * 3 + var col = (y / 3) * 3 + for (i <- row until row + 3; j <- col until col + 3 if board(i)(j) == value) return false + true // 最终没有返回false,就说明该位置可以填写true + } +} +``` + -----------------------
From 730f58bccee54b576c2b5bc998e5b97fe4fb5567 Mon Sep 17 00:00:00 2001 From: hongyang <1664698982@qq.com> Date: Thu, 9 Jun 2022 23:24:30 +0800 Subject: [PATCH 070/114] refactor: add golang solution to Intersection of Two Linked Lists LCCI --- problems/面试题02.07.链表相交.md | 53 +++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 0a38cc33..f603925d 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -13,21 +13,21 @@ 图示两个链表在节点 c1 开始相交: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 -注意,函数返回结果后,链表必须 保持其原始结构 。 +注意,函数返回结果后,链表必须 保持其原始结构 。 -示例 1: +示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) -示例 3: +示例 3: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) @@ -100,7 +100,7 @@ public: ## 其他语言版本 -### Java +### Java ```Java public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { @@ -144,11 +144,11 @@ public class Solution { } return null; } - + } ``` -### Python +### Python ```python class Solution: @@ -162,15 +162,15 @@ class Solution: """ cur_a, cur_b = headA, headB # 用两个指针代替a和b - + while cur_a != cur_b: cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走 cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a - + return cur_a ``` -### Go +### Go ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -### javaScript +递归版本: + +```go +func getIntersectionNode(headA, headB *ListNode) *ListNode { + l1,l2 := headA, headB + for l1 != l2 { + if l1 != nil { + l1 = l1.Next + } else { + l1 = headB + } + + if l2 != nil { + l2 = l2.Next + } else { + l2 = headA + } + } + + return l1 +} +``` + +### javaScript ```js var getListLen = function(head) { @@ -218,9 +241,9 @@ var getListLen = function(head) { cur = cur.next; } return len; -} +} var getIntersectionNode = function(headA, headB) { - let curA = headA,curB = headB, + let curA = headA,curB = headB, lenA = getListLen(headA), lenB = getListLen(headB); if(lenA < lenB) { From ddcd31b2a7668214e94763709d2cb1c88a731dae Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 10 Jun 2022 11:27:26 +0800 Subject: [PATCH 071/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880844.?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index dccc5404..017d4cef 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -399,6 +399,71 @@ var backspaceCompare = function(s, t) { ``` +### TypeScript + +> 双栈法: + +```typescript +function backspaceCompare(s: string, t: string): boolean { + const stack1: string[] = [], + stack2: string[] = []; + for (let c of s) { + if (c === '#') { + stack1.pop(); + } else { + stack1.push(c); + } + } + for (let c of t) { + if (c === '#') { + stack2.pop(); + } else { + stack2.push(c); + } + } + if (stack1.length !== stack2.length) return false; + for (let i = 0, length = stack1.length; i < length; i++) { + if (stack1[i] !== stack2[i]) return false; + } + return true; +}; +``` + +> 双指针法: + +```typescript +function backspaceCompare(s: string, t: string): boolean { + let sIndex: number = s.length - 1, + tIndex: number = t.length - 1; + while (true) { + sIndex = getIndexAfterDel(s, sIndex); + tIndex = getIndexAfterDel(t, tIndex); + if (sIndex < 0 || tIndex < 0) break; + if (s[sIndex] !== t[tIndex]) return false; + sIndex--; + tIndex--; + } + return sIndex === -1 && tIndex === -1; +}; +function getIndexAfterDel(s: string, startIndex: number): number { + let backspaceNum: number = 0; + while (startIndex >= 0) { + // 不可消除 + if (s[startIndex] !== '#' && backspaceNum === 0) break; + // 可消除 + if (s[startIndex] === '#') { + backspaceNum++; + } else { + backspaceNum--; + } + startIndex--; + } + return startIndex; +} +``` + + + -----------------------
From 18f29ef1786e41fba506bc27f35c6a83a116ba14 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 10 Jun 2022 16:28:33 +0800 Subject: [PATCH 072/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 17db4a85..443ab6d7 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -296,5 +296,26 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){ } ``` +### Scala + +```scala +object Solution { + def findContentChildren(g: Array[Int], s: Array[Int]): Int = { + var result = 0 + var children = g.sorted + var cookie = s.sorted + // 遍历饼干 + var j = 0 + for (i <- cookie.indices) { + if (j < children.size && cookie(i) >= children(j)) { + j += 1 + result += 1 + } + } + result + } +} +``` + -----------------------
From 7fd311f47bf257787caeea3bcbf73ec3ce29e978 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 10 Jun 2022 20:09:14 +0800 Subject: [PATCH 073/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 6822896e..fb4d6eff 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -375,7 +375,31 @@ function wiggleMaxLength(nums: number[]): number { }; ``` +### Scala +```scala +object Solution { + def wiggleMaxLength(nums: Array[Int]): Int = { + if (nums.length <= 1) return nums.length + var result = 1 + var curDiff = 0 // 当前一对的差值 + var preDiff = 0 // 前一对的差值 + + for (i <- 1 until nums.length) { + curDiff = nums(i) - nums(i - 1) // 计算当前这一对的差值 + // 当 curDiff > 0 的情况,preDiff <= 0 + // 当 curDiff < 0 的情况,preDiff >= 0 + // 这两种情况算是两个峰值 + if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) { + result += 1 // 结果集加 1 + preDiff = curDiff // 当前差值赋值给上一轮 + } + } + + result + } +} +``` -----------------------
From 7ee790f4f532e56664642163a4612c7242f0fe11 Mon Sep 17 00:00:00 2001 From: ccchooko <648646891@qq.com> Date: Fri, 10 Jun 2022 22:34:11 +0800 Subject: [PATCH 074/114] =?UTF-8?q?0042=20=E6=8E=A5=E9=9B=A8=E6=B0=B4?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E8=B0=83=E6=A0=88go=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0042.接雨水.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index b232ce22..e331967f 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -640,8 +640,44 @@ func min(a,b int)int{ } ``` +单调栈解法 +```go +func trap(height []int) int { + if len(height) <= 2 { + return 0 + } + st := make([]int, 1, len(height)) // 切片模拟单调栈,st存储的是高度数组下标 + var res int + for i := 1; i < len(height); i++ { + if height[i] < height[st[len(st)-1]] { + st = append(st, i) + } else if height[i] == height[st[len(st)-1]] { + st = st[:len(st)-1] // 比较的新元素和栈顶的元素相等,去掉栈中的,入栈新元素下标 + st = append(st, i) + } else { + for len(st) != 0 && height[i] > height[st[len(st)-1]] { + top := st[len(st)-1] + st = st[:len(st)-1] + if len(st) != 0 { + tmp := (min(height[i], height[st[len(st)-1]]) - height[top]) * (i - st[len(st)-1] - 1) + res += tmp + } + } + st = append(st, i) + } + } + return res +} +func min(x, y int) int { + if x >= y { + return y + } + return x +} +``` + ### JavaScript: ```javascript From 9a068272ce8fd245d0c0ef93aee689afe4ea3cce Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 10 Jun 2022 22:46:32 +0800 Subject: [PATCH 075/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880129.?= =?UTF-8?q?=E6=B1=82=E6=A0=B9=E5=88=B0=E5=8F=B6=E5=AD=90=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0129.求根到叶子节点数字之和.md | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index b271ca7d..a34e6921 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -289,7 +289,40 @@ var sumNumbers = function(root) { }; ``` +TypeScript: + +```typescript +function sumNumbers(root: TreeNode | null): number { + if (root === null) return 0; + let resTotal: number = 0; + const route: number[] = []; + route.push(root.val); + recur(root, route); + return resTotal; + function recur(node: TreeNode, route: number[]): void { + if (node.left === null && node.right === null) { + resTotal += listToSum(route); + return; + } + if (node.left !== null) { + route.push(node.left.val); + recur(node.left, route); + route.pop(); + }; + if (node.right !== null) { + route.push(node.right.val); + recur(node.right, route); + route.pop(); + }; + } + function listToSum(nums: number[]): number { + return Number(nums.join('')); + } +}; +``` + C: + ```c //sum记录总和 int sum; From 192beffb66d20fb51cdb61f11649e5cff6379bb8 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 10 Jun 2022 23:38:32 +0800 Subject: [PATCH 076/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881382.?= =?UTF-8?q?=E5=B0=86=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98?= =?UTF-8?q?=E5=B9=B3=E8=A1=A1.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1382.将二叉搜索树变平衡.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 57231ec4..d4d60ebd 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -148,6 +148,30 @@ var balanceBST = function(root) { }; ``` +TypeScript: + +```typescript +function balanceBST(root: TreeNode | null): TreeNode | null { + const inorderArr: number[] = []; + inorderTraverse(root, inorderArr); + return buildTree(inorderArr, 0, inorderArr.length - 1); +}; +function inorderTraverse(node: TreeNode | null, arr: number[]): void { + if (node === null) return; + inorderTraverse(node.left, arr); + arr.push(node.val); + inorderTraverse(node.right, arr); +} +function buildTree(arr: number[], left: number, right: number): TreeNode | null { + if (left > right) return null; + const mid = (left + right) >> 1; + const resNode: TreeNode = new TreeNode(arr[mid]); + resNode.left = buildTree(arr, left, mid - 1); + resNode.right = buildTree(arr, mid + 1, right); + return resNode; +} +``` + ----------------------- From 2386694b5b67e1ec17e42ce2c1f9df784eaeab24 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 11 Jun 2022 00:43:30 +0800 Subject: [PATCH 077/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880100.?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84=E6=A0=91.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0100.相同的树.md | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 5e805d01..d2431f39 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -240,6 +240,46 @@ Go: JavaScript: +TypeScript: + +> 递归法-先序遍历 + +```typescript +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (p === null && q === null) return true; + if (p === null || q === null) return false; + if (p.val !== q.val) return false; + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +}; +``` + +> 迭代法-层序遍历 + +```typescript +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + const queue1: (TreeNode | null)[] = [], + queue2: (TreeNode | null)[] = []; + queue1.push(p); + queue2.push(q); + while (queue1.length > 0 && queue2.length > 0) { + const node1 = queue1.shift(), + node2 = queue2.shift(); + if (node1 === null && node2 === null) continue; + if ( + (node1 === null || node2 === null) || + node1!.val !== node2!.val + ) return false; + queue1.push(node1!.left); + queue1.push(node1!.right); + queue2.push(node2!.left); + queue2.push(node2!.right); + } + return true; +}; +``` + + + -----------------------
From bd4f69d04b663dae39ab5a384722cd7d5aae3377 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 11 Jun 2022 16:35:06 +0800 Subject: [PATCH 078/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=88=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和(动态规划).md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md index 4c883cb6..6712d0ac 100644 --- a/problems/0053.最大子序和(动态规划).md +++ b/problems/0053.最大子序和(动态规划).md @@ -186,7 +186,22 @@ const maxSubArray = nums => { }; ``` +Scala: +```scala +object Solution { + def maxSubArray(nums: Array[Int]): Int = { + var dp = new Array[Int](nums.length) + var result = nums(0) + dp(0) = nums(0) + for (i <- 1 until nums.length) { + dp(i) = math.max(nums(i), dp(i - 1) + nums(i)) + result = math.max(result, dp(i)) // 更新最大值 + } + result + } +} +``` -----------------------
From 9031523bbb334cb2190b17f12333d3a5f1edda53 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 11 Jun 2022 16:40:33 +0800 Subject: [PATCH 079/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0053.最大子序和.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 73cac244..4971133f 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -333,8 +333,41 @@ function maxSubArray(nums: number[]): number { }; ``` +### Scala +**贪心** +```scala +object Solution { + def maxSubArray(nums: Array[Int]): Int = { + var result = Int.MinValue + var count = 0 + for (i <- nums.indices) { + count += nums(i) // count累加 + if (count > result) result = count // 记录最大值 + if (count <= 0) count = 0 // 一旦count为负,则count归0 + } + result + } +} +``` + +**动态规划** + +```scala +object Solution { + def maxSubArray(nums: Array[Int]): Int = { + var dp = new Array[Int](nums.length) + var result = nums(0) + dp(0) = nums(0) + for (i <- 1 until nums.length) { + dp(i) = math.max(nums(i), dp(i - 1) + nums(i)) + result = math.max(result, dp(i)) // 更新最大值 + } + result + } +} +``` -----------------------
From ff233a8e3ccfb08e82ebaa662c93bf687b76f153 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 11 Jun 2022 16:53:35 +0800 Subject: [PATCH 080/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 35 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 1e7b77d8..1369ff5b 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -133,8 +133,9 @@ public: ## 其他语言版本 -Java: +### Java: +贪心: ```java // 贪心思路 class Solution { @@ -148,6 +149,7 @@ class Solution { } ``` +动态规划: ```java class Solution { // 动态规划 public int maxProfit(int[] prices) { @@ -169,8 +171,8 @@ class Solution { // 动态规划 } ``` -Python: - +### Python: +贪心: ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -180,7 +182,7 @@ class Solution: return result ``` -python动态规划 +动态规划: ```python class Solution: def maxProfit(self, prices: List[int]) -> int: @@ -194,7 +196,7 @@ class Solution: return dp[-1][1] ``` -Go: +### Go: ```golang //贪心算法 @@ -231,7 +233,7 @@ func maxProfit(prices []int) int { } ``` -Javascript: +### Javascript: 贪心 ```Javascript @@ -268,7 +270,7 @@ const maxProfit = (prices) => { }; ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(prices: number[]): number { @@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number { }; ``` -C: +### C: 贪心: ```c int maxProfit(int* prices, int pricesSize){ @@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){ } ``` +### Scala + +贪心: +```scala +object Solution { + def maxProfit(prices: Array[Int]): Int = { + var result = 0 + for (i <- 1 until prices.length) { + if (prices(i) > prices(i - 1)) { + result += prices(i) - prices(i - 1) + } + } + result + } +} +``` + -----------------------
From b0664fcb81c475356ddf925b839df6e00c434e8a Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 11 Jun 2022 17:21:03 +0800 Subject: [PATCH 081/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0055.跳跃游戏.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md index 17a3b4f4..345f8eba 100644 --- a/problems/0055.跳跃游戏.md +++ b/problems/0055.跳跃游戏.md @@ -193,7 +193,22 @@ function canJump(nums: number[]): boolean { }; ``` - +### Scala +```scala +object Solution { + def canJump(nums: Array[Int]): Boolean = { + var cover = 0 + if (nums.length == 1) return true // 如果只有一个元素,那么必定到达 + var i = 0 + while (i <= cover) { // i表示下标,当前只能够走cover步 + cover = math.max(i + nums(i), cover) + if (cover >= nums.length - 1) return true // 说明可以覆盖到终点,直接返回 + i += 1 + } + false // 如果上面没有返回就是跳不到 + } +} +``` ----------------------- From ad24b1fb1fa50e568a7357bbe772df59fc008e74 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 11 Jun 2022 18:29:48 +0800 Subject: [PATCH 082/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880116.?= =?UTF-8?q?=E5=A1=AB=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E4=B8=8B=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=8C=87=E9=92=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0116.填充每个节点的下一个右侧节点指针.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md index 2c443de5..1e5b2271 100644 --- a/problems/0116.填充每个节点的下一个右侧节点指针.md +++ b/problems/0116.填充每个节点的下一个右侧节点指针.md @@ -287,6 +287,79 @@ const connect = root => { }; ``` +## TypeScript + +(注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’) + +> 递归法: + +```typescript +class NodePro { + val: number + left: NodePro | null + right: NodePro | null + next: NodePro | null + constructor(val?: number, left?: NodePro, right?: NodePro, next?: NodePro) { + this.val = (val === undefined ? 0 : val) + this.left = (left === undefined ? null : left) + this.right = (right === undefined ? null : right) + this.next = (next === undefined ? null : next) + } +} + +function connect(root: NodePro | null): NodePro | null { + if (root === null) return null; + root.next = null; + recur(root); + return root; +}; +function recur(node: NodePro): void { + if (node.left === null || node.right === null) return; + node.left.next = node.right; + node.right.next = node.next && node.next.left; + recur(node.left); + recur(node.right); +} +``` + +> 迭代法: + +```typescript +class NodePro { + val: number + left: NodePro | null + right: NodePro | null + next: NodePro | null + constructor(val?: number, left?: NodePro, right?: NodePro, next?: NodePro) { + this.val = (val === undefined ? 0 : val) + this.left = (left === undefined ? null : left) + this.right = (right === undefined ? null : right) + this.next = (next === undefined ? null : next) + } +} + +function connect(root: NodePro | null): NodePro | null { + if (root === null) return null; + const queue: NodePro[] = []; + queue.push(root); + while (queue.length > 0) { + for (let i = 0, length = queue.length; i < length; i++) { + const curNode: NodePro = queue.shift()!; + if (i === length - 1) { + curNode.next = null; + } else { + curNode.next = queue[0]; + } + if (curNode.left !== null) queue.push(curNode.left); + if (curNode.right !== null) queue.push(curNode.right); + } + } + return root; +}; +``` + + + -----------------------
From 74a422c53b2817271b8f50a61bdd8ccdf77a5e25 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 11 Jun 2022 20:03:12 +0800 Subject: [PATCH 083/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 4e3ab24a..f2940361 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -279,7 +279,31 @@ function jump(nums: number[]): number { }; ``` +### Scala +```scala +object Solution { + def jump(nums: Array[Int]): Int = { + if (nums.length == 0) return 0 + var result = 0 // 记录走的最大步数 + var curDistance = 0 // 当前覆盖最远距离下标 + var nextDistance = 0 // 下一步覆盖最远距离下标 + for (i <- nums.indices) { + nextDistance = math.max(nums(i) + i, nextDistance) // 更新下一步覆盖最远距离下标 + if (i == curDistance) { + if (curDistance != nums.length - 1) { + result += 1 + curDistance = nextDistance + if (nextDistance >= nums.length - 1) return result + } else { + return result + } + } + } + result + } +} +``` From 89c9044bf984ad638d07932f3b9ebe91e2e7589d Mon Sep 17 00:00:00 2001 From: xiaojun <13589818805@163.com> Date: Thu, 23 Jun 2022 15:36:55 +0800 Subject: [PATCH 084/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881382.=20?= =?UTF-8?q?=E5=B0=86=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98?= =?UTF-8?q?=E5=B9=B3=E8=A1=A1=EF=BC=89=E7=9A=84go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1382.将二叉搜索树变平衡.md | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md index 57231ec4..7c7f8484 100644 --- a/problems/1382.将二叉搜索树变平衡.md +++ b/problems/1382.将二叉搜索树变平衡.md @@ -123,6 +123,46 @@ class Solution: ``` Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func balanceBST(root *TreeNode) *TreeNode { + // 二叉搜索树中序遍历得到有序数组 + nums := []int{} + // 中序递归遍历二叉树 + var travel func(node *TreeNode) + travel = func(node *TreeNode) { + if node == nil { + return + } + travel(node.Left) + nums = append(nums, node.Val) + travel(node.Right) + } + // 二分法保证左右子树高度差不超过一(题目要求返回的仍是二叉搜索树) + var buildTree func(nums []int, left, right int) *TreeNode + buildTree = func(nums []int, left, right int) *TreeNode { + if left > right { + return nil + } + mid := left + (right-left) >> 1 + root := &TreeNode{Val: nums[mid]} + root.Left = buildTree(nums, left, mid-1) + root.Right = buildTree(nums, mid+1, right) + return root + } + travel(root) + return buildTree(nums, 0, len(nums)-1) +} + +``` + JavaScript: ```javascript var balanceBST = function(root) { From beb6805c1d7fd77b66ba0870870fd12228d5a4cf Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 23 Jun 2022 10:29:24 +0100 Subject: [PATCH 085/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201049.=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E9=87=8F.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1049.最后一块石头的重量II.md | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index ee0ddef2..c49f0a18 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -277,5 +277,36 @@ var lastStoneWeightII = function (stones) { }; ``` +C版本 +```c +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +int getSum(int *stones, int stoneSize) { + int sum = 0, i; + for (i = 0; i < stoneSize; ++i) + sum += stones[i]; + return sum; +} + +int lastStoneWeightII(int* stones, int stonesSize){ + int sum = getSum(stones, stonesSize); + int target = sum / 2; + int i, j; + + // 初始化dp数组 + int *dp = (int*)malloc(sizeof(int) * (target + 1)); + memset(dp, 0, sizeof(int) * (target + 1)); + for (j = stones[0]; j <= target; ++j) + dp[j] = stones[0]; + + // 递推公式:dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) + for (i = 1; i < stonesSize; ++i) { + for (j = target; j >= stones[i]; --j) + dp[j] = MAX(dp[j], dp[j - stones[i]] + stones[i]); + } + return sum - dp[target] - dp[target]; +} +``` + -----------------------
From ffe981fb6c5af5c7400f3b82c455b80ac8333fc0 Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Sun, 26 Jun 2022 23:50:32 +0800 Subject: [PATCH 086/114] =?UTF-8?q?0977.=20=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20=E6=B7=BB=E5=8A=A0Java?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 20bdf7b0..d274d778 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -106,6 +106,7 @@ class Solution { int index = result.length - 1; while (left <= right) { if (nums[left] * nums[left] > nums[right] * nums[right]) { + // 正数的相对位置是不变的, 需要调整的是负数平方后的相对位置 result[index--] = nums[left] * nums[left]; ++left; } else { From 16af63e22ad0d3522c19ccf5eb3dd4a0b4294a9e Mon Sep 17 00:00:00 2001 From: Shuo Zhang Date: Mon, 27 Jun 2022 22:54:52 -0400 Subject: [PATCH 087/114] =?UTF-8?q?=E4=BF=AE=E6=94=B90349=20-=20=E7=94=A8?= =?UTF-8?q?=20Java=20stream=20=E4=BB=A3=E6=9B=BF=20for=20loop=20(=E8=BF=99?= =?UTF-8?q?=E6=A0=B7=E4=B8=BB=E9=A2=98=E6=9B=B4=E6=98=8E=E6=98=BE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 98518647..2a8b2dae 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -104,13 +104,8 @@ class Solution { resSet.add(i); } } - int[] resArr = new int[resSet.size()]; - int index = 0; //将结果几何转为数组 - for (int i : resSet) { - resArr[index++] = i; - } - return resArr; + return resSet.stream().mapToInt(x -> x).toArray(); } } ``` From 0cd4ffd9a470038f1b77207af082a47c00e8d430 Mon Sep 17 00:00:00 2001 From: azou Date: Wed, 29 Jun 2022 23:32:29 +0800 Subject: [PATCH 088/114] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0TS=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 --- problems/0203.移除链表元素.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index b79d29a5..0e461ce8 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -397,18 +397,18 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { ```typescript function removeElements(head: ListNode | null, val: number): ListNode | null { - let dummyHead = new ListNode(0, head); - let pre: ListNode = dummyHead, cur: ListNode | null = dummyHead.next; - // 删除非头部节点 + // 添加虚拟节点 + const data = new ListNode(0, head); + let pre = data, cur = data.next; while (cur) { if (cur.val === val) { - pre.next = cur.next; + pre.next = cur.next } else { pre = cur; } cur = cur.next; } - return head.next; + return data.next; }; ``` From 55086c231acbb8794299d2f143444173de2b85e9 Mon Sep 17 00:00:00 2001 From: w2xi <43wangxi@gmail.com> Date: Sat, 2 Jul 2022 11:14:57 +0800 Subject: [PATCH 089/114] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20JavaScript=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 8e35fc9d..83d20df8 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -470,25 +470,14 @@ func invertTree(root *TreeNode) *TreeNode { 使用递归版本的前序遍历 ```javascript var invertTree = function(root) { - //1. 首先使用递归版本的前序遍历实现二叉树翻转 - //交换节点函数 - const inverNode=function(left,right){ - let temp=left; - left=right; - right=temp; - //需要重新给root赋值一下 - root.left=left; - root.right=right; + // 终止条件 + if (!root) { + return null; } - //确定递归函数的参数和返回值inverTree=function(root) - //确定终止条件 - if(root===null){ - return root; - } - //确定节点处理逻辑 交换 - inverNode(root.left,root.right); - invertTree(root.left); - invertTree(root.right); + // 交换左右节点 + const rightNode = root.right; + root.right = invertTree(root.left); + root.left = invertTree(rightNode); return root; }; ``` From 889256a3781ebc8891f8a37778334f402acf362e Mon Sep 17 00:00:00 2001 From: chenwingsing <742474834@qq.com> Date: Sat, 2 Jul 2022 21:58:17 +0800 Subject: [PATCH 090/114] =?UTF-8?q?=E6=9B=B4=E6=96=B00018JAVA=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用例有一个是[1000000000,1000000000,1000000000,1000000000] -294967296 如果用int会越界,所以修改为long --- problems/0018.四数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 2146a114..99613070 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -153,7 +153,7 @@ class Solution { int left = j + 1; int right = nums.length - 1; while (right > left) { - int sum = nums[i] + nums[j] + nums[left] + nums[right]; + long sum = (long) nums[i] + nums[j] + nums[left] + nums[right]; if (sum > target) { right--; } else if (sum < target) { From 1965ff69f70373fd48f31c590e77152444ceefe3 Mon Sep 17 00:00:00 2001 From: chenwingsing <742474834@qq.com> Date: Sun, 3 Jul 2022 09:07:51 +0800 Subject: [PATCH 091/114] =?UTF-8?q?=E6=9B=B4=E6=96=B0[0015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C]=E8=A7=A3=E9=A2=98=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加逗号让解题更加清晰,避免歧义,我一开始就看成a = num[i] *b了 --- problems/0015.三数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 4f1d711a..8bc8dd91 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -95,7 +95,7 @@ public: 拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。 -依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i] b = nums[left] c = nums[right]。 +依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i],b = nums[left],c = nums[right]。 接下来如何移动left 和right呢, 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。 From 8c54205566578bf331587de936e85b5bc4454e5a Mon Sep 17 00:00:00 2001 From: chenwingsing <742474834@qq.com> Date: Sun, 3 Jul 2022 11:53:38 +0800 Subject: [PATCH 092/114] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错误字 --- problems/0151.翻转字符串里的单词.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 38372f91..23f966e8 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -79,7 +79,7 @@ void removeExtraSpaces(string& s) { 逻辑很简单,从前向后遍历,遇到空格了就erase。 -如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是O(n)的时间复杂度呢。 +如果不仔细琢磨一下erase的时间复杂度,还以为以上的代码是O(n)的时间复杂度呢。 想一下真正的时间复杂度是多少,一个erase本来就是O(n)的操作,erase实现原理题目:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html),最优的算法来移除元素也要O(n)。 From 11b701fdb6ba689bdc9f07362e497a4d2ab5eefd Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Mon, 4 Jul 2022 00:58:56 +0800 Subject: [PATCH 093/114] =?UTF-8?q?0018=20Java=E4=BB=A3=E7=A0=81=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=A2=9E=E5=8A=A0=E5=89=AA=E6=9E=9D=E6=93=8D=E4=BD=9C?= =?UTF-8?q?,=E4=B8=8D=E7=84=B6Leetcode=E4=B8=8D=E8=83=BD=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0018.四数之和.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 6cbd40c2..d0b7fc68 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -140,6 +140,11 @@ class Solution { for (int i = 0; i < nums.length; i++) { + // nums[i] > target 直接返回, 剪枝操作 + if (nums[i] > 0 && nums[i] > target) { + return result; + } + if (i > 0 && nums[i - 1] == nums[i]) { continue; } From ca00ec680557689d6b302fe27b3e66e479a0d421 Mon Sep 17 00:00:00 2001 From: xiaojun <13589818805@163.com> Date: Mon, 4 Jul 2022 15:43:35 +0800 Subject: [PATCH 094/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0031.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=8E=92=E5=88=97.md)go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0031.下一个排列.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index bce8adef..1a3641b0 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -171,13 +171,13 @@ class Solution(object): i = n-2 while i >= 0 and nums[i] >= nums[i+1]: i -= 1 - + if i > -1: // i==-1,不存在下一个更大的排列 j = n-1 while j >= 0 and nums[j] <= nums[i]: j -= 1 nums[i], nums[j] = nums[j], nums[i] - + start, end = i+1, n-1 while start < end: nums[start], nums[end] = nums[end], nums[start] @@ -190,6 +190,26 @@ class Solution(object): ## Go ```go +//卡尔的解法 +func nextPermutation(nums []int) { + for i:=len(nums)-1;i>=0;i--{ + for j:=len(nums)-1;j>i;j--{ + if nums[j]>nums[i]{ + //交换 + nums[j],nums[i]=nums[i],nums[j] + reverse(nums,0+i+1,len(nums)-1) + return + } + } + } + reverse(nums,0,len(nums)-1) +} +//对目标切片指定区间的反转方法 +func reverse(a []int,begin,end int){ + for i,j:=begin,end;i Date: Wed, 6 Jul 2022 22:01:14 +0800 Subject: [PATCH 095/114] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md=20JavaScript=E9=80=92=E5=BD=92=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0450.删除二叉搜索树中的节点.md | 57 +++++++++++++++---------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 3fa2a1c5..3bc8369c 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -456,31 +456,42 @@ func deleteNode(root *TreeNode, key int) *TreeNode { * @param {number} key * @return {TreeNode} */ -var deleteNode = function (root, key) { - if (root === null) - return root; - if (root.val === key) { - if (!root.left) - return root.right; - else if (!root.right) - return root.left; - else { - let cur = root.right; - while (cur.left) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - delete root; - return root; - } - } - if (root.val > key) - root.left = deleteNode(root.left, key); - if (root.val < key) +var deleteNode = function(root, key) { + if (!root) return null; + if (key > root.val) { root.right = deleteNode(root.right, key); - return root; + return root; + } else if (key < root.val) { + root.left = deleteNode(root.left, key); + return root; + } else { + // 场景1: 该节点是叶节点 + if (!root.left && !root.right) { + return null + } + // 场景2: 有一个孩子节点不存在 + if (root.left && !root.right) { + return root.left; + } else if (root.right && !root.left) { + return root.right; + } + // 场景3: 左右节点都存在 + const rightNode = root.right; + // 获取最小值节点 + const minNode = getMinNode(rightNode); + // 将待删除节点的值替换为最小值节点值 + root.val = minNode.val; + // 删除最小值节点 + root.right = deleteNode(root.right, minNode.val); + return root; + } }; +function getMinNode(root) { + while (root.left) { + root = root.left; + } + return root; +} ``` 迭代 From 0c6b1db35cc3c5fc6d227026e4b5175628003318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 7 Jul 2022 09:00:14 +0800 Subject: [PATCH 096/114] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index a788939b..31458d84 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -106,12 +106,9 @@ public: // 如果index大于链表的长度,则返回空 // 如果index小于0,则置为0,作为链表的新头节点。 void addAtIndex(int index, int val) { - if (index > _size) { + if (index > _size || index < 0) { return; } - else if (index < 0) { - index = 0; - } LinkedNode* newNode = new LinkedNode(val); LinkedNode* cur = _dummyHead; while(index--) { From 0f3ffb3e282af527cd2a529d7921a16321c2561e Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Fri, 8 Jul 2022 10:58:53 +0800 Subject: [PATCH 097/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0746.使用最小花费爬楼梯.md Rust版本 --- problems/0746.使用最小花费爬楼梯.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index c2c73715..0006f7ac 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -288,6 +288,24 @@ function minCostClimbingStairs(cost: number[]): number { }; ``` +### Rust + +```Rust +use std::cmp::min; +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let len = cost.len(); + let mut dp = vec![0; len]; + dp[0] = cost[0]; + dp[1] = cost[1]; + for i in 2..len { + dp[i] = min(dp[i-1], dp[i-2]) + cost[i]; + } + min(dp[len-1], dp[len-2]) + } +} +``` + ### C ```c From 9b3468e3b3e7ff844bc1c8d1a5bbb141072d5689 Mon Sep 17 00:00:00 2001 From: Ezralin <10881430+ezralin@user.noreply.gitee.com> Date: Fri, 8 Jul 2022 14:10:18 +0800 Subject: [PATCH 098/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A00034=20Kotlin?= =?UTF-8?q?=E7=89=88,0203=20Kotlin=E7=89=88,0209=20Koltin=E7=89=88,=200977?= =?UTF-8?q?=20Kotlin=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...排序数组中查找元素的第一个和最后一个位置.md | 43 +++++++++++++++++++ problems/0203.移除链表元素.md | 34 +++++++++++++++ problems/0209.长度最小的子数组.md | 32 ++++++++++++++ problems/0977.有序数组的平方.md | 28 ++++++++++++ 4 files changed, 137 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index a81b3641..6922c399 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -584,5 +584,48 @@ object Solution { ``` +### Kotlin +```kotlin +class Solution { + fun searchRange(nums: IntArray, target: Int): IntArray { + var index = binarySearch(nums, target) + // 没找到,返回[-1, -1] + if (index == -1) return intArrayOf(-1, -1) + var left = index + var right = index + // 寻找左边界 + while (left - 1 >=0 && nums[left - 1] == target){ + left-- + } + // 寻找右边界 + while (right + 1 target) { + right = middle - 1 + } + else { + if (nums[middle] < target) { + left = middle + 1 + } + else { + return middle + } + } + } + // 没找到,返回-1 + return -1 + } +} +``` + -----------------------
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index b79d29a5..ea936093 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -532,5 +532,39 @@ object Solution { } } ``` +Kotlin: +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun removeElements(head: ListNode?, `val`: Int): ListNode? { + // 使用虚拟节点,令该节点指向head + var dummyNode = ListNode(-1) + dummyNode.next = head + // 使用cur遍历链表各个节点 + var cur = dummyNode + // 判断下个节点是否为空 + while (cur.next != null) { + // 符合条件,移除节点 + if (cur.next.`val` == `val`) { + cur.next = cur.next.next + } + // 不符合条件,遍历下一节点 + else { + cur = cur.next + } + } + // 注意:返回的不是虚拟节点 + return dummyNode.next + } +} +``` -----------------------
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 090e73f0..c1633def 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -417,6 +417,38 @@ class Solution { } } ``` +滑动窗口 +```kotlin +class Solution { + fun minSubArrayLen(target: Int, nums: IntArray): Int { + // 左边界 和 右边界 + var left: Int = 0 + var right: Int = 0 + // sum 用来记录和 + var sum: Int = 0 + // result记录一个固定值,便于判断是否存在的这样的数组 + var result: Int = Int.MAX_VALUE + // subLenth记录长度 + var subLength = Int.MAX_VALUE + + + while (right < nums.size) { + // 从数组首元素开始逐次求和 + sum += nums[right++] + // 判断 + while (sum >= target) { + var temp = right - left + // 每次和上一次比较求出最小数组长度 + subLength = if (subLength > temp) temp else subLength + // sum减少,左边界右移 + sum -= nums[left++] + } + } + // 如果subLength为初始值,则说明长度为0,否则返回subLength + return if(subLength == result) 0 else subLength + } +} +``` Scala: 滑动窗口: diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 458107dd..d46b5259 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -362,6 +362,8 @@ class Solution { ``` Kotlin: + +双指针法 ```kotlin class Solution { // 双指针法 @@ -383,6 +385,32 @@ class Solution { } } ``` +骚操作(暴力思路) +```kotlin +class Solution { + fun sortedSquares(nums: IntArray): IntArray { + // left 与 right 用来控制循环,类似于滑动窗口 + var left: Int = 0; + var right: Int = nums.size - 1; + // 将每个数字的平方经过排序后加入result数值 + var result: IntArray = IntArray(nums.size); + var k: Int = nums.size - 1; + while (left <= right) { + // 从大到小,从后向前填满数组 + // [left, right] 控制循环 + if (nums[left] * nums[left] > nums[right] * nums[right]) { + result[k--] = nums[left] * nums[left] + left++ + } + else { + result[k--] = nums[right] * nums[right] + right-- + } + } + return result + } +} +``` Scala: From a8897483eade1262627d360544e7022951162243 Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Fri, 8 Jul 2022 14:40:26 +0800 Subject: [PATCH 099/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200062.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84.md=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0062.不同路径.md Rust版本 --- problems/0062.不同路径.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 02bcc2ae..aa7c8ab8 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -374,6 +374,30 @@ function uniquePaths(m: number, n: number): number { }; ``` +### Rust + +```Rust +impl Solution { + pub fn unique_paths(m: i32, n: i32) -> i32 { + let m = m as usize; + let n = n as usize; + let mut dp = vec![vec![0; n]; m]; + for i in 0..m { + dp[i][0] = 1; + } + for j in 0..n { + dp[0][j] = 1; + } + for i in 1..m { + for j in 1..n { + dp[i][j] = dp[i-1][j] + dp[i][j-1]; + } + } + dp[m-1][n-1] + } +} +``` + ### C ```c From 821cca116c6d42020ddac3bbb68c632eba7de132 Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Fri, 8 Jul 2022 15:11:49 +0800 Subject: [PATCH 100/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0063.不同路径II.md Rust版本 --- problems/0063.不同路径II.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index ca34055a..e3857db6 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -384,6 +384,42 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { }; ``` +### Rust + +```Rust +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let m: usize = obstacle_grid.len(); + let n: usize = obstacle_grid[0].len(); + if obstacle_grid[0][0] == 1 || obstacle_grid[m-1][n-1] == 1 { + return 0; + } + let mut dp = vec![vec![0; n]; m]; + for i in 0..m { + if obstacle_grid[i][0] == 1 { + break; + } + else { dp[i][0] = 1; } + } + for j in 0..n { + if obstacle_grid[0][j] == 1 { + break; + } + else { dp[0][j] = 1; } + } + for i in 1..m { + for j in 1..n { + if obstacle_grid[i][j] == 1 { + continue; + } + dp[i][j] = dp[i-1][j] + dp[i][j-1]; + } + } + dp[m-1][n-1] + } +} +``` + ### C ```c From e2d16c5894b2b9abed99c42522977dac2b6e8654 Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Sun, 10 Jul 2022 09:09:02 +0800 Subject: [PATCH 101/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88=200077.=E7=BB=84=E5=90=88=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E4=BC=98=E5=8C=96=200216.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CI?= =?UTF-8?q?II=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0017.电话号码的字母组合 0077.组合 0077.组合优化 0216.组合总和III Rust版本 --- problems/0017.电话号码的字母组合.md | 43 +++++++++++++++++++++++++ problems/0077.组合.md | 50 +++++++++++++++++++++++++++++ problems/0077.组合优化.md | 26 +++++++++++++++ problems/0216.组合总和III.md | 29 +++++++++++++++++ 4 files changed, 148 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index e357a33b..5778e903 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -454,6 +454,49 @@ function letterCombinations(digits: string): string[] { }; ``` +## Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec, s: &mut String, map: &[&str; 10], digits: &String, index: usize) { + let len = digits.len(); + if len == index { + result.push(s.to_string()); + return; + } + // 在保证不会越界的情况下使用unwrap()将Some()中的值提取出来 + let digit= digits.chars().nth(index).unwrap().to_digit(10).unwrap() as usize; + let letters = map[digit]; + for i in letters.chars() { + s.push(i); + Self::backtracking(result, s, &map, &digits, index+1); + s.pop(); + } + } + pub fn letter_combinations(digits: String) -> Vec { + if digits.len() == 0 { + return vec![]; + } + const MAP: [&str; 10] = [ + "", + "", + "abc", + "def", + "ghi", + "jkl", + "mno", + "pqrs", + "tuv", + "wxyz" + ]; + let mut result: Vec = Vec::new(); + let mut s: String = String::new(); + Self::backtracking(&mut result, &mut s, &MAP, &digits, 0); + result + } +} +``` + ## C ```c diff --git a/problems/0077.组合.md b/problems/0077.组合.md index fc72be15..5a4811ba 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -535,6 +535,56 @@ func backtrack(n,k,start int,track []int){ } ``` +### Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + for i in startIndex..= n { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + +剪枝 +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + // 此处剪枝 + for i in startIndex..= n - (k - len) + 1 { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + ### C ```c int* path; diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 8d742566..e336fb75 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -261,6 +261,32 @@ function combine(n: number, k: number): number[][] { }; ``` +Rust: + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + let len= path.len() as i32; + if len == k{ + result.push(path.to_vec()); + return; + } + // 此处剪枝 + for i in startIndex..= n - (k - len) + 1 { + path.push(i); + Self::backtracking(result, path, n, k, i+1); + path.pop(); + } + } + pub fn combine(n: i32, k: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 1); + result + } +} +``` + C: ```c diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 2c1bf717..0a59e216 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -411,6 +411,35 @@ function combinationSum3(k: number, n: number): number[][] { }; ``` +## Rust + +```Rust +impl Solution { + fn backtracking(result: &mut Vec>, path:&mut Vec, targetSum:i32, k: i32, mut sum: i32, startIndex: i32) { + let len = path.len() as i32; + if len == k { + if sum == targetSum { + result.push(path.to_vec()); + } + return; + } + for i in startIndex..=9 { + sum += i; + path.push(i); + Self::backtracking(result, path, targetSum, k, sum, i+1); + sum -= i; + path.pop(); + } + } + pub fn combination_sum3(k: i32, n: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut path: Vec = Vec::new(); + Self::backtracking(&mut result, &mut path, n, k, 0, 1); + result + } +} +``` + ## C ```c From 85e0d6c85d593339152483a3fea33ab23cdcd5b3 Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Mon, 11 Jul 2022 11:06:51 +0800 Subject: [PATCH 102/114] =?UTF-8?q?=E6=9B=B4=E6=96=B00347=20=E5=89=8DK?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由原来的构建小顶堆改为构建大顶堆,减少部分代码逻辑,并增加了注释 通过LeetCode提交代码,修改后的执行时间更快。 --- problems/0347.前K个高频元素.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index a04041cc..0c978b2a 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -141,13 +141,10 @@ class Solution { } Set> entries = map.entrySet(); - // 根据map的value值正序排,相当于一个小顶堆 - PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue()); + // 根据map的value值,构建于一个大顶堆(o1 - o2: 小顶堆, o2 - o1 : 大顶堆) + PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); for (Map.Entry entry : entries) { queue.offer(entry); - if (queue.size() > k) { - queue.poll(); - } } for (int i = k - 1; i >= 0; i--) { result[i] = queue.poll().getKey(); From c265c9220c62a78109c1f6033d47f9bc8b3f47bd Mon Sep 17 00:00:00 2001 From: BaoTaoqi <464115280@qq.com> Date: Mon, 11 Jul 2022 14:50:03 +0800 Subject: [PATCH 103/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=200344.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=200376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2II=20=E5=89=91=E6=8C=87Offer05.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0203.移除链表元素 0344.反转字符串 0376.摆动序列 0541.反转字符串II 剑指Offer05.替换空格 Rust版本 --- problems/0203.移除链表元素.md | 18 ++++++++++-------- problems/0344.反转字符串.md | 16 ++++++++++++++++ problems/0376.摆动序列.md | 23 +++++++++++++++++++++++ problems/0541.反转字符串II.md | 31 +++++++++++++++++++++++++++++++ problems/剑指Offer05.替换空格.md | 31 +++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 8 deletions(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index b79d29a5..cb46dc70 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -487,17 +487,19 @@ RUST: // } impl Solution { pub fn remove_elements(head: Option>, val: i32) -> Option> { - let mut head = head; - let mut dummy_head = ListNode::new(0); - let mut cur = &mut dummy_head; - while let Some(mut node) = head { - head = std::mem::replace(&mut node.next, None); - if node.val != val { - cur.next = Some(node); + let mut dummyHead = Box::new(ListNode::new(0)); + dummyHead.next = head; + let mut cur = dummyHead.as_mut(); + // 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读 + while let Some(nxt) = cur.next.take() { + if nxt.val == val { + cur.next = nxt.next; + } else { + cur.next = Some(nxt); cur = cur.next.as_mut().unwrap(); } } - dummy_head.next + dummyHead.next } } ``` diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index aba6e2f3..3138f60f 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -238,6 +238,22 @@ func reverseString(_ s: inout [Character]) { ``` +Rust: +```Rust +impl Solution { + pub fn reverse_string(s: &mut Vec) { + let (mut left, mut right) = (0, s.len()-1); + while left < right { + let temp = s[left]; + s[left] = s[right]; + s[right] = temp; + left += 1; + right -= 1; + } + } +} +``` + C: ```c void reverseString(char* s, int sSize){ diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 00f8f70c..33a5282d 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -298,6 +298,29 @@ var wiggleMaxLength = function(nums) { }; ``` +### Rust +**贪心** +```Rust +impl Solution { + pub fn wiggle_max_length(nums: Vec) -> i32 { + let len = nums.len() as usize; + if len <= 1 { + return len as i32; + } + let mut preDiff = 0; + let mut curDiff = 0; + let mut result = 1; + for i in 0..len-1 { + curDiff = nums[i+1] - nums[i]; + if (preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0) { + result += 1; + preDiff = curDiff; + } + } + result + } +} +``` ### C **贪心** diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 84061ef5..7ef6463e 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -389,5 +389,36 @@ object Solution { } } ``` + +Rust: + +```Rust +impl Solution { + pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){ + while begin < end { + let temp = s[begin]; + s[begin] = s[end]; + s[end] = temp; + begin += 1; + end -= 1; + } + } + pub fn reverse_str(s: String, k: i32) -> String { + let len = s.len(); + let k = k as usize; + let mut s = s.chars().collect::>(); + for i in (0..len).step_by(2 * k) { + if i + k < len { + Self::reverse(&mut s, i, i + k - 1); + } + else { + Self::reverse(&mut s, i, len - 1); + } + } + s.iter().collect::() + } +} +``` + -----------------------
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 78b03b17..e1ccc458 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -506,6 +506,37 @@ function spaceLen($s){ } ``` +Rust + +```Rust +impl Solution { + pub fn replace_space(s: String) -> String { + let mut len: usize = s.len(); + let mut s = s.chars().collect::>(); + let mut count = 0; + for i in &s { + if i.is_ascii_whitespace() { + count += 1; + } + } + let mut new_len = len + count * 2; + s.resize(new_len, ' '); + while len < new_len { + len -= 1; + new_len -= 1; + if s[len].is_ascii_whitespace() { + s[new_len] = '0'; + s[new_len - 1] = '2'; + s[new_len - 2] = '%'; + new_len -= 2; + } + else { s[new_len] = s[len] } + } + s.iter().collect::() + } +} +``` + -----------------------
From e0a422859904ddae865cc828bc30d6620ccb8137 Mon Sep 17 00:00:00 2001 From: Ezralin <10881430+ezralin@user.noreply.gitee.com> Date: Mon, 11 Jul 2022 15:09:41 +0800 Subject: [PATCH 104/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A00206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20Kotlin=E8=A7=A3=E6=9E=90=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 24ec7b94..bb9b62d5 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -420,6 +420,41 @@ fun reverseList(head: ListNode?): ListNode? { return pre } ``` +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun reverseList(head: ListNode?): ListNode? { + // temp用来存储临时的节点 + var temp: ListNode? + // cur用来遍历链表 + var cur: ListNode? = head + // pre用来作为链表反转的工具 + // pre是比pre前一位的节点 + var pre: ListNode? = null + while (cur != null) { + // 临时存储原本cur的下一个节点 + temp = cur.next + // 使cur下一节点地址为它之前的 + cur.next = pre + // 之后随着cur的遍历移动pre + pre = cur; + // 移动cur遍历链表各个节点 + cur = temp; + } + // 由于开头使用pre为null,所以cur等于链表本身长度+1, + // 此时pre在cur前一位,所以此时pre为头节点 + return pre; + } +} +``` Swift: ```swift From 4e77e911b397938419a41c2a06684318740fc907 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:42:21 +0800 Subject: [PATCH 105/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0151.翻转字符串里的单词 Rust版本 --- problems/0151.翻转字符串里的单词.md | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 38372f91..552584f5 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -864,7 +864,58 @@ function reverseString(&$s, $start, $end) { return ; } ``` +Rust: +```Rust +// 根据C++版本二思路进行实现 +// 函数名根据Rust编译器建议由驼峰命名法改为蛇形命名法 +impl Solution { + pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){ + while begin < end { + let temp = s[begin]; + s[begin] = s[end]; + s[end] = temp; + begin += 1; + end -= 1; + } +} +pub fn remove_extra_spaces(s: &mut Vec) { + let mut slow: usize = 0; + let len = s.len(); + // 注意这里不能用for循环,不然在里面那个while循环中对i的递增会失效 + let mut i: usize = 0; + while i < len { + if !s[i].is_ascii_whitespace() { + if slow != 0 { + s[slow] = ' '; + slow += 1; + } + while i < len && !s[i].is_ascii_whitespace() { + s[slow] = s[i]; + slow += 1; + i += 1; + } + } + i += 1; + } + s.resize(slow, ' '); + } + pub fn reverse_words(s: String) -> String { + let mut s = s.chars().collect::>(); + Self::remove_extra_spaces(&mut s); + let len = s.len(); + Self::reverse(&mut s, 0, len - 1); + let mut start = 0; + for i in 0..=len { + if i == len || s[i].is_ascii_whitespace() { + Self::reverse(&mut s, start, i - 1); + start = i + 1; + } + } + s.iter().collect::() + } +} +``` -----------------------
From ba081f84a85ff75134c4db551e0e631b86693081 Mon Sep 17 00:00:00 2001 From: AronJudge <2286381138@qq.com> Date: Tue, 12 Jul 2022 11:36:26 +0800 Subject: [PATCH 106/114] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码错误,更正代码,通过LeetCode代码检验。 --- problems/0104.二叉树的最大深度.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index ed27f95d..55980189 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -294,14 +294,13 @@ class solution { /** * 递归法 */ - public int maxdepth(treenode root) { + public int maxDepth(TreeNode root) { if (root == null) { return 0; } - int leftdepth = maxdepth(root.left); - int rightdepth = maxdepth(root.right); - return math.max(leftdepth, rightdepth) + 1; - + int leftDepth = maxDepth(root.left); + int rightDepth = maxDepth(root.right); + return Math.max(leftDepth, rightDepth) + 1; } } ``` @@ -311,23 +310,23 @@ class solution { /** * 迭代法,使用层序遍历 */ - public int maxdepth(treenode root) { + public int maxDepth(TreeNode root) { if(root == null) { return 0; } - deque deque = new linkedlist<>(); + Deque deque = new LinkedList<>(); deque.offer(root); int depth = 0; - while (!deque.isempty()) { + while (!deque.isEmpty()) { int size = deque.size(); depth++; for (int i = 0; i < size; i++) { - treenode poll = deque.poll(); - if (poll.left != null) { - deque.offer(poll.left); + TreeNode node = deque.poll(); + if (node.left != null) { + deque.offer(node.left); } - if (poll.right != null) { - deque.offer(poll.right); + if (node.right != null) { + deque.offer(node.right); } } } From a8e19d60bb371a44296134932b7470825c1fda6a Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Tue, 12 Jul 2022 14:08:50 +0800 Subject: [PATCH 107/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0202.快乐数 Rust版本 --- problems/0202.快乐数.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 0bea0c72..2303765c 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -315,6 +315,36 @@ class Solution { } ``` +Rust: +```Rust +use std::collections::HashSet; +impl Solution { + pub fn get_sum(mut n: i32) -> i32 { + let mut sum = 0; + while n > 0 { + sum += (n % 10) * (n % 10); + n /= 10; + } + sum + } + + pub fn is_happy(n: i32) -> bool { + let mut n = n; + let mut set = HashSet::new(); + loop { + let sum = Self::get_sum(n); + if sum == 1 { + return true; + } + if set.contains(&sum) { + return false; + } else { set.insert(sum); } + n = sum; + } + } +} +``` + C: ```C typedef struct HashNodeTag { From 07ab44ede3fa043184754a638f59e6b76a7c3e0a Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Tue, 12 Jul 2022 15:26:07 +0800 Subject: [PATCH 108/114] =?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=200018.=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0015.三数之和 0018.四数之和 Rust版本 --- problems/0015.三数之和.md | 65 +++++++++++++++++++++++++++++++++++++++ problems/0018.四数之和.md | 45 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 4f1d711a..94885b0c 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -554,6 +554,71 @@ func threeSum(_ nums: [Int]) -> [[Int]] { } ``` +Rust: +```Rust +// 哈希解法 +use std::collections::HashSet; +impl Solution { + pub fn three_sum(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for i in 0..len { + if nums[i] > 0 { break; } + if i > 0 && nums[i] == nums[i - 1] { continue; } + let mut set = HashSet::new(); + for j in (i + 1)..len { + if j > i + 2 && nums[j] == nums[j - 1] && nums[j] == nums[j - 2] { continue; } + let c = 0 - (nums[i] + nums[j]); + if set.contains(&c) { + result.push(vec![nums[i], nums[j], c]); + set.remove(&c); + } else { set.insert(nums[j]); } + } + } + result + } +} +``` + +```Rust +// 双指针法 +use std::collections::HashSet; +impl Solution { + pub fn three_sum(nums: Vec) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for i in 0..len { + if nums[i] > 0 { return result; } + if i > 0 && nums[i] == nums[i - 1] { continue; } + let (mut left, mut right) = (i + 1, len - 1); + while left < right { + if nums[i] + nums[left] + nums[right] > 0 { + right -= 1; + // 去重 + while left < right && nums[right] == nums[right + 1] { right -= 1; } + } else if nums[i] + nums[left] + nums[right] < 0 { + left += 1; + // 去重 + while left < right && nums[left] == nums[left - 1] { left += 1; } + } else { + result.push(vec![nums[i], nums[left], nums[right]]); + // 去重 + right -= 1; + left += 1; + while left < right && nums[right] == nums[right + 1] { right -= 1; } + while left < right && nums[left] == nums[left - 1] { left += 1; } + } + } + } + result + } +} +``` + C: ```C //qsort辅助cmp函数 diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 2146a114..f6cfad29 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -522,6 +522,51 @@ public class Solution } } ``` + +Rust: +```Rust +impl Solution { + pub fn four_sum(nums: Vec, target: i32) -> Vec> { + let mut result: Vec> = Vec::new(); + let mut nums = nums; + nums.sort(); + let len = nums.len(); + for k in 0..len { + // 剪枝 + if nums[k] > target && (nums[k] > 0 || target > 0) { break; } + // 去重 + if k > 0 && nums[k] == nums[k - 1] { continue; } + for i in (k + 1)..len { + // 剪枝 + if nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0) { break; } + // 去重 + if i > k + 1 && nums[i] == nums[i - 1] { continue; } + let (mut left, mut right) = (i + 1, len - 1); + while left < right { + if nums[k] + nums[i] > target - (nums[left] + nums[right]) { + right -= 1; + // 去重 + while left < right && nums[right] == nums[right + 1] { right -= 1; } + } else if nums[k] + nums[i] < target - (nums[left] + nums[right]) { + left += 1; + // 去重 + while left < right && nums[left] == nums[left - 1] { left += 1; } + } else { + result.push(vec![nums[k], nums[i], nums[left], nums[right]]); + // 去重 + while left < right && nums[right] == nums[right - 1] { right -= 1; } + while left < right && nums[left] == nums[left + 1] { left += 1; } + left += 1; + right -= 1; + } + } + } + } + result + } +} +``` + Scala: ```scala object Solution { From 0c1c77c23412b3a8ebba93fde9017ca8d820936e Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Wed, 13 Jul 2022 19:27:34 +0800 Subject: [PATCH 109/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87?= =?UTF-8?q?Offer=2058-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 剑指Offer 58-II.左旋转字符串 Rust版本 --- problems/剑指Offer58-II.左旋转字符串.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 4674c141..de4a9030 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -341,7 +341,30 @@ object Solution { } ``` +Rust: +```Rust +impl Solution { + pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){ + while begin < end { + let temp = s[begin]; + s[begin] = s[end]; + s[end] = temp; + begin += 1; + end -= 1; + } + } + pub fn reverse_left_words(s: String, n: i32) -> String { + let len = s.len(); + let mut s = s.chars().collect::>(); + let n = n as usize; + Self::reverse(&mut s, 0, n - 1); + Self::reverse(&mut s, n, len - 1); + Self::reverse(&mut s, 0, len - 1); + s.iter().collect::() + } +} +``` From acd71b8b9e134fdc1fbe0f73d78b5f80b7b03a59 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Thu, 14 Jul 2022 15:46:41 +0800 Subject: [PATCH 110/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200028.=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr=200459.=E9=87=8D=E5=A4=8D=E7=9A=84=E5=AD=90?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0028.实现strStr 0459.重复的子字符串 Rust版本 --- problems/0028.实现strStr.md | 44 +++++++++++++++++++++++++++++++++ problems/0459.重复的子字符串.md | 32 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index a95a52f8..1887e91b 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -1241,5 +1241,49 @@ function getNext(&$next, $s){ } } ``` + +Rust: + +> 前缀表统一不减一 +```Rust +impl Solution { + pub fn get_next(next: &mut Vec, s: &Vec) { + let len = s.len(); + let mut j = 0; + for i in 1..len { + while j > 0 && s[i] != s[j] { + j = next[j - 1]; + } + if s[i] == s[j] { + j += 1; + } + next[i] = j; + } + } + + pub fn str_str(haystack: String, needle: String) -> i32 { + let (haystack_len, needle_len) = (haystack.len(), needle.len()); + if haystack_len == 0 { return 0; } + if haystack_len < needle_len { return -1;} + let (haystack, needle) = (haystack.chars().collect::>(), needle.chars().collect::>()); + let mut next: Vec = vec![0; haystack_len]; + Self::get_next(&mut next, &needle); + let mut j = 0; + for i in 0..haystack_len { + while j > 0 && haystack[i] != needle[j] { + j = next[j - 1]; + } + if haystack[i] == needle[j] { + j += 1; + } + if j == needle_len { + return (i - needle_len + 1) as i32; + } + } + return -1; + } +} +``` + -----------------------
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index bb55bd7c..4f45f4d7 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -505,5 +505,37 @@ Swift: } ``` +Rust: + +>前缀表统一不减一 +```Rust +impl Solution { + pub fn get_next(next: &mut Vec, s: &Vec) { + let len = s.len(); + let mut j = 0; + for i in 1..len { + while j > 0 && s[i] != s[j] { + j = next[j - 1]; + } + if s[i] == s[j] { + j += 1; + } + next[i] = j; + } + } + + pub fn repeated_substring_pattern(s: String) -> bool { + let s = s.chars().collect::>(); + let len = s.len(); + if len == 0 { return false; }; + let mut next = vec![0; len]; + Self::get_next(&mut next, &s); + if next[len - 1] != 0 && len % (len - (next[len - 1] )) == 0 { return true; } + return false; + } +} +``` + + -----------------------
From 781076a73d7d9de9f0fe525cbc8404358ca59990 Mon Sep 17 00:00:00 2001 From: Tristone Date: Thu, 14 Jul 2022 22:39:17 +0800 Subject: [PATCH 111/114] =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BE=93=E5=85=A5=E6=95=B0=E7=BB=84nums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 9d687cfb..fd24e8f8 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -219,6 +219,7 @@ func removeElement(nums []int, val int) int { res++ } } + nums=nums[:res] return res } ``` From 26f5e59cdaf5536e0b08b94d554e4ef4428732a7 Mon Sep 17 00:00:00 2001 From: leo <55868230+LIU-HONGYANG@users.noreply.github.com> Date: Sat, 16 Jul 2022 11:56:18 +0800 Subject: [PATCH 112/114] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.?= =?UTF-8?q?07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index f603925d..d799bc80 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -208,7 +208,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -递归版本: +双指针 ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { From 6f3dc1e6f80c5527d266c125b1a57366d314787a Mon Sep 17 00:00:00 2001 From: DarrenRuan <31378679+DarrenRuan@users.noreply.github.com> Date: Sat, 16 Jul 2022 23:57:33 -0400 Subject: [PATCH 113/114] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这里python代码有个注释似乎写错了。原来为`不是i-1`,应该是`不是i+1`. --- problems/0039.组合总和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 564d13ea..54e9f2e5 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -291,7 +291,7 @@ class Solution: for i in range(start_index, len(candidates)): sum_ += candidates[i] self.path.append(candidates[i]) - self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1 + self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i+1 sum_ -= candidates[i] # 回溯 self.path.pop() # 回溯 ``` From 50b9753b647ace27caa0a1a31bd91975672db4d7 Mon Sep 17 00:00:00 2001 From: dcj_hp <294487055@qq.com> Date: Wed, 20 Jul 2022 01:11:00 +0800 Subject: [PATCH 114/114] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20=20=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=E7=89=88=E6=9C=AC(Python,=20C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 70a62145..714dba57 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -228,6 +228,8 @@ class Solution { ### Python +**贪心** + ```python class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: @@ -240,7 +242,30 @@ class Solution: return res ``` +**动态规划** + +```python +class Solution: + def wiggleMaxLength(self, nums: List[int]) -> int: + # 0 i 作为波峰的最大长度 + # 1 i 作为波谷的最大长度 + # dp是一个列表,列表中每个元素是长度为 2 的列表 + dp = [] + for i in range(len(nums)): + # 初始为[1, 1] + dp.append([1, 1]) + for j in range(i): + # nums[i] 为波谷 + if nums[j] > nums[i]: + dp[i][1] = max(dp[i][1], dp[j][0] + 1) + # nums[i] 为波峰 + if nums[j] < nums[i]: + dp[i][0] = max(dp[i][0], dp[j][1] + 1) + return max(dp[-1][0], dp[-1][1]) +``` + ### Go + ```golang func wiggleMaxLength(nums []int) int { var count,preDiff,curDiff int @@ -324,6 +349,7 @@ impl Solution { ### C **贪心** + ```c int wiggleMaxLength(int* nums, int numsSize){ if(numsSize <= 1) @@ -349,6 +375,44 @@ int wiggleMaxLength(int* nums, int numsSize){ } ``` +**动态规划** + +```c +int max(int left, int right) +{ + return left > right ? left : right; +} +int wiggleMaxLength(int* nums, int numsSize){ + if(numsSize <= 1) + { + return numsSize; + } + // 0 i 作为波峰的最大长度 + // 1 i 作为波谷的最大长度 + int dp[numsSize][2]; + for(int i = 0; i < numsSize; i++) + { + dp[i][0] = 1; + dp[i][1] = 1; + for(int j = 0; j < i; j++) + { + // nums[i] 为山谷 + if(nums[j] > nums[i]) + { + dp[i][1] = max(dp[i][1], dp[j][0] + 1); + } + // nums[i] 为山峰 + if(nums[j] < nums[i]) + { + dp[i][0] = max(dp[i][0], dp[j][1] + 1); + } + } + } + return max(dp[numsSize - 1][0], dp[numsSize - 1][1]); +} +``` + + ### TypeScript