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 01/14] =?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 --- ...538.把二叉搜索树转换为累加树.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 02/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=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 03/14] =?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 04/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=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/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 05/14] =?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 06/14] =?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 07/14] =?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 08/14] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复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 09/14] =?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 10/14] =?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 11/14] =?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 12/14] =?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 13/14] =?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 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 14/14] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/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--) {