@@ -693,5 +693,37 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
693
693
}
694
694
```
695
695
696
+
697
+ ## Scala
698
+
699
+ ``` scala
700
+ object Solution {
701
+ import scala .collection .mutable
702
+ def combinationSum2 (candidates : Array [Int ], target : Int ): List [List [Int ]] = {
703
+ var res = mutable.ListBuffer [List [Int ]]()
704
+ var path = mutable.ListBuffer [Int ]()
705
+ var candidate = candidates.sorted
706
+
707
+ def backtracking (sum : Int , startIndex : Int ): Unit = {
708
+ if (sum == target) {
709
+ res.append(path.toList)
710
+ return
711
+ }
712
+
713
+ for (i <- startIndex until candidate.size if sum + candidate(i) <= target) {
714
+ if (! (i > startIndex && candidate(i) == candidate(i - 1 ))) {
715
+ path.append(candidate(i))
716
+ backtracking(sum + candidate(i), i + 1 )
717
+ path = path.take(path.size - 1 )
718
+ }
719
+ }
720
+ }
721
+
722
+ backtracking(0 , 0 )
723
+ res.toList
724
+ }
725
+ }
726
+ ```
727
+
696
728
-----------------------
697
729
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
0 commit comments