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 377116d

Browse files
committed
add: 46
1 parent d415f92 commit 377116d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎problems/0046.permute/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
# 0046.permute
3+
4+
```
5+
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
6+
7+
8+
9+
示例 1:
10+
11+
输入:nums = [1,2,3]
12+
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
13+
示例 2:
14+
15+
输入:nums = [0,1]
16+
输出:[[0,1],[1,0]]
17+
示例 3:
18+
19+
输入:nums = [1]
20+
输出:[[1]]
21+
22+
23+
提示:
24+
25+
1 <= nums.length <= 6
26+
-10 <= nums[i] <= 10
27+
nums 中的所有整数 互不相同
28+
```

‎problems/0046.permute/run.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package permute
2+
3+
import "fmt"
4+
5+
func Run() {
6+
arr := []int{1, 2, 3}
7+
res := permute(arr)
8+
fmt.Println(res)
9+
}
10+
11+
func permute(nums []int) [][]int {
12+
l := len(nums)
13+
if l == 0 {
14+
return nil
15+
}
16+
res := make([][]int, 0)
17+
used := make([]bool, l)
18+
path := make([]int, l)
19+
df := func(int) {}
20+
df = func(i int) {
21+
if i == l {
22+
res = append(res, append([]int(nil), path...))
23+
return
24+
}
25+
for k, find := range used {
26+
if find {
27+
continue
28+
}
29+
path[i] = nums[k]
30+
used[k] = true
31+
df(i + 1)
32+
used[k] = false
33+
}
34+
}
35+
df(0)
36+
return res
37+
}

‎problems/0046.permute/run_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package permute
3+
4+
import "testing"
5+
6+
func TestRun(t *testing.T) {
7+
Run()
8+
}

0 commit comments

Comments
(0)

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