26
26
27
27
<!-- 这里可写通用的实现逻辑 -->
28
28
29
+ 深度优先搜索。
30
+
29
31
<!-- tabs:start -->
30
32
31
33
### ** Python3**
32
34
33
35
<!-- 这里可写当前语言的特殊实现逻辑 -->
34
36
35
- 回溯法:
36
-
37
37
``` python
38
38
class Solution :
39
39
def permute (self , nums : List[int ]) -> List[List[int ]]:
40
- def dfs (nums , i , res , path , used ):
41
- if i == len (nums):
42
- res.append(copy.deepcopy(path))
43
- return
44
- for j in range (len (nums)):
45
- if not used[j]:
46
- path.append(nums[j])
47
- used[j] = True
48
- dfs(nums, i + 1 , res, path, used)
49
- used[j] = False
50
- path.pop()
51
-
52
- res, path = [], []
53
- used = [False ] * len (nums)
54
- dfs(nums, 0 , res, path, used)
55
- return res
56
- ```
57
-
58
- 切分数组:
59
-
60
- ``` python
61
- class Solution :
62
- def permute (self , nums : List[int ]) -> List[List[int ]]:
63
- if len (nums) <= 1 :
64
- return [nums]
40
+ n = len (nums)
65
41
res = []
66
- for i, num in enumerate (nums):
67
- n = nums[:i] + nums[i + 1 :]
68
- for item in self .permute(n):
69
- res.append([num] + item)
42
+ path = [0 ] * n
43
+ used = [False ] * n
44
+
45
+ def dfs (u ):
46
+ if u == n:
47
+ res.append(path.copy())
48
+ return
49
+ for i in range (n):
50
+ if not used[i]:
51
+ path[u] = nums[i]
52
+ used[i] = True
53
+ dfs(u + 1 )
54
+ used[i] = False
55
+
56
+ dfs(0 )
70
57
return res
71
58
```
72
59
73
60
### ** Java**
74
61
75
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
63
77
- 回溯法:
78
-
79
64
``` java
80
65
class Solution {
81
66
public List<List<Integer > > permute (int [] nums ) {
82
67
List<List<Integer > > res = new ArrayList<> ();
83
68
List<Integer > path = new ArrayList<> ();
84
- boolean [] used = new boolean [nums. length];
85
- dfs(nums, 0 , res, path, used);
69
+ int n = nums. length;
70
+ boolean [] used = new boolean [n];
71
+ dfs(0 , n, nums, used, path, res);
86
72
return res;
87
73
}
88
74
89
- private void dfs (int [] nums , int i , List<List< Integer >> res , List<Integer > path , boolean [] used ) {
90
- if (i == nums . length ) {
75
+ private void dfs (int u , int n , int [] nums , boolean [] used , List<Integer > path , List<List< Integer >> res ) {
76
+ if (u == n ) {
91
77
res. add(new ArrayList<> (path));
92
78
return ;
93
79
}
94
- for (int j = 0 ; j < nums . length ; ++ j ) {
95
- if (! used[j ]) {
96
- path. add(nums[j ]);
97
- used[j ] = true ;
98
- dfs(nums, i + 1 , res, path , used);
99
- used[j ] = false ;
80
+ for (int i = 0 ; i < n ; ++ i ) {
81
+ if (! used[i ]) {
82
+ path. add(nums[i ]);
83
+ used[i ] = true ;
84
+ dfs(u + 1 , n, nums , used, path, res );
85
+ used[i ] = false ;
100
86
path. remove(path. size() - 1 );
101
87
}
102
88
}
103
89
}
104
90
}
105
91
```
106
92
107
- - 递归:
108
-
109
- ``` java
110
- class Solution {
111
- public List<List<Integer > > permute (int [] nums ) {
112
- List<List<Integer > > res = new ArrayList<> ();
113
- permute(res, nums, 0 );
114
- return res;
115
- }
116
-
117
- private void permute (List<List<Integer > > res , int [] nums , int start ) {
118
- if (start == nums. length) {
119
- List<Integer > t = new ArrayList<> ();
120
- for (int e : nums) {
121
- t. add(e);
122
- }
123
- res. add(t);
124
- return ;
125
- }
126
- for (int i = start; i < nums. length; ++ i) {
127
- swap(nums, i, start);
128
- permute(res, nums, start + 1 );
129
- swap(nums, i, start);
130
- }
131
- }
132
-
133
- private void swap (int [] nums , int i , int j ) {
134
- int t = nums[i];
135
- nums[i] = nums[j];
136
- nums[j] = t;
137
- }
138
- }
139
- ```
140
-
141
93
### ** JavaScript**
142
94
143
95
``` js
@@ -146,30 +98,95 @@ class Solution {
146
98
* @return {number[][]}
147
99
*/
148
100
var permute = function (nums ) {
101
+ const n = nums .length ;
149
102
let res = [];
150
- let solution = [];
151
- let record = new Array (nums . length ).fill (false );
152
- dfs (nums, 0 , record, solution , res);
103
+ let path = [];
104
+ let used = new Array (n ).fill (false );
105
+ dfs (0 , n, nums, used, path , res);
153
106
return res;
154
107
};
155
108
156
- function dfs ( nums , depth , record , solution , res ) {
157
- if (depth == nums . length ) {
158
- res .push (solution .slice ());
109
+ function dfs ( u , n , nums , used , path , res ) {
110
+ if (u == n ) {
111
+ res .push (path .slice ());
159
112
return ;
160
113
}
161
- for (let i = 0 ; i < nums . length ; i ++ ) {
162
- if (! record [i]) {
163
- solution .push (nums[i]);
164
- record [i] = true ;
165
- dfs (nums, depth + 1 , record, solution , res);
166
- solution . pop () ;
167
- record[i] = false ;
114
+ for (let i = 0 ; i < n; ++ i ) {
115
+ if (! used [i]) {
116
+ path .push (nums[i]);
117
+ used [i] = true ;
118
+ dfs (u + 1 , n, nums, used, path , res);
119
+ used[i] = false ;
120
+ path . pop () ;
168
121
}
169
122
}
170
123
}
171
124
```
172
125
126
+ ### ** C++**
127
+
128
+ ``` cpp
129
+ class Solution {
130
+ public:
131
+ vector<vector<int >> permute(vector<int >& nums) {
132
+ int n = nums.size();
133
+ vector<vector<int >> res;
134
+ vector<int > path(n, 0);
135
+ vector<bool > used(n, false);
136
+ dfs(0, n, nums, used, path, res);
137
+ return res;
138
+ }
139
+
140
+ void dfs(int u, int n, vector<int>& nums, vector<bool>& used, vector<int>& path, vector<vector<int>>& res) {
141
+ if (u == n)
142
+ {
143
+ res.emplace_back(path);
144
+ return;
145
+ }
146
+ for (int i = 0 ; i < n; ++i)
147
+ {
148
+ if (!used[i])
149
+ {
150
+ path[u] = nums[i];
151
+ used[i] = true;
152
+ dfs (u + 1, n, nums, used, path, res);
153
+ used[ i] = false;
154
+ }
155
+ }
156
+ }
157
+ };
158
+ ```
159
+
160
+ ### ** Go**
161
+
162
+ ``` go
163
+ func permute (nums []int ) [][]int {
164
+ n := len (nums)
165
+ res := make ([][]int , 0 )
166
+ path := make ([]int , n)
167
+ used := make ([]bool , n)
168
+ dfs (0 , n, nums, used, path, &res)
169
+ return res
170
+ }
171
+
172
+ func dfs (u , n int , nums []int , used []bool , path []int , res *[][]int ) {
173
+ if u == n {
174
+ t := make ([]int , n)
175
+ copy (t, path)
176
+ *res = append (*res, t)
177
+ return
178
+ }
179
+ for i := 0 ; i < n; i++ {
180
+ if !used[i] {
181
+ path[u] = nums[i]
182
+ used[i] = true
183
+ dfs (u+1 , n, nums, used, path, res)
184
+ used[i] = false
185
+ }
186
+ }
187
+ }
188
+ ```
189
+
173
190
### ** ...**
174
191
175
192
```
0 commit comments