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 068cc09

Browse files
committed
添加 0491.递增子序列.md Scala版本
1 parent b353018 commit 068cc09

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎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)

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