Skip to content

Commit 02d318d

Browse files
Merge pull request youngyangyang04#1443 from wzqwtt/backtracking05
添加(0078.子集、0090.子集II、0491.递增子序列) Scala版本
2 parents 7eeea2e + 068cc09 commit 02d318d

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

problems/0078.子集.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,60 @@ func subsets(_ nums: [Int]) -> [[Int]] {
373373
}
374374
```
375375

376+
## Scala
377+
378+
思路一: 使用本题解思路
379+
380+
```scala
381+
object Solution {
382+
import scala.collection.mutable
383+
def subsets(nums: Array[Int]): List[List[Int]] = {
384+
var result = mutable.ListBuffer[List[Int]]()
385+
var path = mutable.ListBuffer[Int]()
386+
387+
def backtracking(startIndex: Int): Unit = {
388+
result.append(path.toList) // 存放结果
389+
if (startIndex >= nums.size) {
390+
return
391+
}
392+
for (i <- startIndex until nums.size) {
393+
path.append(nums(i)) // 添加元素
394+
backtracking(i + 1)
395+
path.remove(path.size - 1) // 删除
396+
}
397+
}
398+
399+
backtracking(0)
400+
result.toList
401+
}
402+
}
403+
```
404+
405+
思路二: 将原问题转换为二叉树,针对每一个元素都有**选或不选**两种选择,直到遍历到最后,所有的叶子节点即为本题的答案:
406+
407+
```scala
408+
object Solution {
409+
import scala.collection.mutable
410+
def subsets(nums: Array[Int]): List[List[Int]] = {
411+
var result = mutable.ListBuffer[List[Int]]()
412+
413+
def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = {
414+
if (startIndex == nums.length) {
415+
result.append(path.toList)
416+
return
417+
}
418+
path.append(nums(startIndex))
419+
backtracking(path, startIndex + 1) // 选择元素
420+
path.remove(path.size - 1)
421+
backtracking(path, startIndex + 1) // 不选择元素
422+
}
423+
424+
backtracking(mutable.ListBuffer[Int](), 0)
425+
result.toList
426+
}
427+
}
428+
```
429+
376430

377431
-----------------------
378432
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0090.子集II.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,63 @@ func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
434434
}
435435
```
436436

437+
### Scala
438+
439+
不使用userd数组:
440+
441+
```scala
442+
object Solution {
443+
import scala.collection.mutable
444+
def subsetsWithDup(nums: Array[Int]): List[List[Int]] = {
445+
var result = mutable.ListBuffer[List[Int]]()
446+
var path = mutable.ListBuffer[Int]()
447+
var num = nums.sorted // 排序
448+
449+
def backtracking(startIndex: Int): Unit = {
450+
result.append(path.toList)
451+
if (startIndex >= num.size){
452+
return
453+
}
454+
for (i <- startIndex until num.size) {
455+
// 同一树层重复的元素不进入回溯
456+
if (!(i > startIndex && num(i) == num(i - 1))) {
457+
path.append(num(i))
458+
backtracking(i + 1)
459+
path.remove(path.size - 1)
460+
}
461+
}
462+
}
463+
464+
backtracking(0)
465+
result.toList
466+
}
467+
}
468+
```
469+
470+
使用Set去重:
471+
```scala
472+
object Solution {
473+
import scala.collection.mutable
474+
def subsetsWithDup(nums: Array[Int]): List[List[Int]] = {
475+
var result = mutable.Set[List[Int]]()
476+
var num = nums.sorted
477+
def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = {
478+
if (startIndex == num.length) {
479+
result.add(path.toList)
480+
return
481+
}
482+
path.append(num(startIndex))
483+
backtracking(path, startIndex + 1) // 选择
484+
path.remove(path.size - 1)
485+
backtracking(path, startIndex + 1) // 不选择
486+
}
487+
488+
backtracking(mutable.ListBuffer[Int](), 0)
489+
490+
result.toList
491+
}
492+
}
493+
```
437494

438495
-----------------------
439496
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0491.递增子序列.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,5 +522,39 @@ func findSubsequences(_ nums: [Int]) -> [[Int]] {
522522
```
523523

524524

525+
## Scala
526+
527+
```scala
528+
object Solution {
529+
import scala.collection.mutable
530+
def findSubsequences(nums: Array[Int]): List[List[Int]] = {
531+
var result = mutable.ListBuffer[List[Int]]()
532+
var path = mutable.ListBuffer[Int]()
533+
534+
def backtracking(startIndex: Int): Unit = {
535+
// 集合元素大于1,添加到结果集
536+
if (path.size > 1) {
537+
result.append(path.toList)
538+
}
539+
540+
var used = new Array[Boolean](201)
541+
// 使用循环守卫,当前层没有用过的元素才有资格进入回溯
542+
for (i <- startIndex until nums.size if !used(nums(i) + 100)) {
543+
// 如果path没元素或 当前循环的元素比path的最后一个元素大,则可以进入回溯
544+
if (path.size == 0 || (!path.isEmpty && nums(i) >= path(path.size - 1))) {
545+
used(nums(i) + 100) = true
546+
path.append(nums(i))
547+
backtracking(i + 1)
548+
path.remove(path.size - 1)
549+
}
550+
}
551+
}
552+
553+
backtracking(0)
554+
result.toList
555+
}
556+
}
557+
```
558+
525559
-----------------------
526560
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)