mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0047.全排列II.md Go 版本
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
</p>
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 排列问题(二)
|
||||
|
||||
## 47.全排列 II
|
||||
@ -222,6 +223,43 @@ class Solution:
|
||||
return res
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
var res [][]int
|
||||
func permute(nums []int) [][]int {
|
||||
res = [][]int{}
|
||||
sort.Ints(nums)
|
||||
dfs(nums, make([]int, 0), make([]bool, len(nums)))
|
||||
return res
|
||||
}
|
||||
|
||||
func dfs(nums, path []int, used []bool) {
|
||||
if len(path) == len(nums) {
|
||||
res = append(res, append([]int{}, path...))
|
||||
return
|
||||
}
|
||||
|
||||
m := make(map[int]bool)
|
||||
for i := 0; i < len(nums); i++ {
|
||||
// used 从剩余 nums 中选
|
||||
if used[i] {
|
||||
continue
|
||||
}
|
||||
// m 集合间去重
|
||||
if _, ok := m[nums[i]]; ok {
|
||||
continue
|
||||
}
|
||||
m[nums[i]] = true
|
||||
path = append(path, nums[i])
|
||||
used[i] = true
|
||||
dfs(nums, path, used)
|
||||
used[i] = false
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
|
||||
```javascript
|
||||
|
Reference in New Issue
Block a user