mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #1448 from wzqwtt/backtracking06
添加(0046.全排列、0047.全排列II) Scala版本
This commit is contained in:
@ -456,6 +456,36 @@ func permute(_ nums: [Int]) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def permute(nums: Array[Int]): List[List[Int]] = {
|
||||||
|
var result = mutable.ListBuffer[List[Int]]()
|
||||||
|
var path = mutable.ListBuffer[Int]()
|
||||||
|
|
||||||
|
def backtracking(used: Array[Boolean]): Unit = {
|
||||||
|
if (path.size == nums.size) {
|
||||||
|
// 如果path的长度和nums相等,那么可以添加到结果集
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 添加循环守卫,只有当当前数字没有用过的情况下才进入回溯
|
||||||
|
for (i <- nums.indices if used(i) == false) {
|
||||||
|
used(i) = true
|
||||||
|
path.append(nums(i))
|
||||||
|
backtracking(used) // 回溯
|
||||||
|
path.remove(path.size - 1)
|
||||||
|
used(i) = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(new Array[Boolean](nums.size)) // 调用方法
|
||||||
|
result.toList // 最终返回结果集的List形式
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -422,5 +422,43 @@ int** permuteUnique(int* nums, int numsSize, int* returnSize, int** returnColumn
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def permuteUnique(nums: Array[Int]): List[List[Int]] = {
|
||||||
|
var result = mutable.ListBuffer[List[Int]]()
|
||||||
|
var path = mutable.ListBuffer[Int]()
|
||||||
|
var num = nums.sorted // 首先对数据进行排序
|
||||||
|
|
||||||
|
def backtracking(used: Array[Boolean]): Unit = {
|
||||||
|
if (path.size == num.size) {
|
||||||
|
// 如果path的size等于num了,那么可以添加到结果集
|
||||||
|
result.append(path.toList)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 循环守卫,当前元素没被使用过就进入循环体
|
||||||
|
for (i <- num.indices if used(i) == false) {
|
||||||
|
// 当前索引为0,不存在和前一个数字相等可以进入回溯
|
||||||
|
// 当前索引值和上一个索引不相等,可以回溯
|
||||||
|
// 前一个索引对应的值没有被选,可以回溯
|
||||||
|
// 因为Scala没有continue,只能将逻辑反过来写
|
||||||
|
if (i == 0 || (i > 0 && num(i) != num(i - 1)) || used(i-1) == false) {
|
||||||
|
used(i) = true
|
||||||
|
path.append(num(i))
|
||||||
|
backtracking(used)
|
||||||
|
path.remove(path.size - 1)
|
||||||
|
used(i) = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
backtracking(new Array[Boolean](nums.length))
|
||||||
|
result.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user