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

[pull] master from youngyangyang04:master #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 17 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
ca18b7d
添加 0538.把二叉搜索树转换为累加树.md Scala版本
wzqwtt May 31, 2022
752cfda
添加(0027.移除元素.md):增加 C# 版本
Cwlrin May 31, 2022
3992de9
Merge branch 'youngyangyang04:master' into master
Cwlrin Jun 1, 2022
f51d72b
添加(0239.滑动窗口最大值.md):PHP版本
fmtvar Jun 1, 2022
9007512
添加(0977.有序数组的平方.md):增加 C# 版本
Cwlrin Jun 1, 2022
0bdb37d
添加 0077.组合.md Scala版本
wzqwtt Jun 2, 2022
e734d0a
添加 0077.组合.md 剪枝 Scala版本
wzqwtt Jun 2, 2022
5ac414f
添加 0077.组合优化.md Scala版本
wzqwtt Jun 2, 2022
3d5f379
Merge branch 'master' into master
Cwlrin Jun 2, 2022
6eb31be
Merge branch 'youngyangyang04:master' into master
Cwlrin Jun 4, 2022
8e76783
Merge branch 'youngyangyang04:master' into master
Cwlrin Jun 6, 2022
0312262
Merge branch 'master' into master
Cwlrin Jun 27, 2022
200315d
Merge pull request #1423 from wzqwtt/tree18
youngyangyang04 Jul 6, 2022
69143a0
Merge pull request #1424 from Cwlrin/master
youngyangyang04 Jul 6, 2022
0a04d2b
Merge branch 'master' into 0150
youngyangyang04 Jul 6, 2022
75fb3fb
Merge pull request #1425 from fmtvar/0150
youngyangyang04 Jul 6, 2022
1cdd50c
Merge pull request #1427 from wzqwtt/backtracking01
youngyangyang04 Jul 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions problems/0077.组合.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -673,5 +673,63 @@ 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关键字可以省略
}
}
```

剪枝:

```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关键字可以省略
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
29 changes: 29 additions & 0 deletions problems/0077.组合优化.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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关键字可以省略
}
}
```

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

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