Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 301a703

Browse files
committed
添加 0040.组合总和II.md Scala版本
1 parent d2e9927 commit 301a703

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎problems/0040.组合总和II.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,5 +693,37 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
693693
}
694694
```
695695

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+
696728
-----------------------
697729
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /