@@ -45,7 +45,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
45
45
46
46
### 方法一:DFS(回溯)
47
47
48
- 我们设计一个函数 $dfs(i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
48
+ 我们设计一个函数 $\textit{ dfs} (i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
49
49
50
50
时间复杂度 $O(n \times n!)$,其中 $n$ 是字符串的长度。一共有 $n!$ 个排列,每个排列需要 $O(n)$ 的时间来构造。
51
51
@@ -57,22 +57,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
57
57
class Solution :
58
58
def permutation (self , S : str ) -> List[str ]:
59
59
def dfs (i : int ):
60
- if i = = n:
60
+ if i > = n:
61
61
ans.append(" " .join(t))
62
62
return
63
63
for j, c in enumerate (S):
64
- if vis[j]:
65
- continue
66
- vis[j] = True
67
- t.append(c)
68
- dfs(i + 1 )
69
- t.pop()
70
- vis[j] = False
64
+ if not vis[j]:
65
+ vis[j] = True
66
+ t[i] = c
67
+ dfs(i + 1 )
68
+ vis[j] = False
71
69
70
+ ans = []
72
71
n = len (S)
73
72
vis = [False ] * n
74
- ans = []
75
- t = []
73
+ t = list (S)
76
74
dfs(0 )
77
75
return ans
78
76
```
@@ -82,30 +80,31 @@ class Solution:
82
80
``` java
83
81
class Solution {
84
82
private char [] s;
85
- private boolean [] vis = new boolean [' z' + 1 ];
83
+ private char [] t;
84
+ private boolean [] vis;
86
85
private List<String > ans = new ArrayList<> ();
87
- private StringBuilder t = new StringBuilder ();
88
86
89
87
public String [] permutation (String S ) {
90
88
s = S . toCharArray();
89
+ int n = s. length;
90
+ vis = new boolean [n];
91
+ t = new char [n];
91
92
dfs(0 );
92
93
return ans. toArray(new String [0 ]);
93
94
}
94
95
95
96
private void dfs (int i ) {
96
- if (i = = s. length) {
97
- ans. add(t . toString( ));
97
+ if (i > = s. length) {
98
+ ans. add(new String (t ));
98
99
return ;
99
100
}
100
- for (char c : s) {
101
- if (vis[c]) {
102
- continue ;
101
+ for (int j = 0 ; j < s. length; ++ j) {
102
+ if (! vis[j]) {
103
+ vis[j] = true ;
104
+ t[i] = s[j];
105
+ dfs(i + 1 );
106
+ vis[j] = false ;
103
107
}
104
- vis[c] = true ;
105
- t. append(c);
106
- dfs(i + 1 );
107
- t. deleteCharAt(t. length() - 1 );
108
- vis[c] = false ;
109
108
}
110
109
}
111
110
}
@@ -119,51 +118,49 @@ public:
119
118
vector<string > permutation(string S) {
120
119
int n = S.size();
121
120
vector<bool > vis(n);
121
+ string t = S;
122
122
vector<string > ans;
123
- string t;
124
- function<void(int)> dfs = [ &] (int i) {
123
+ auto dfs = [ &] (this auto&& dfs, int i) {
125
124
if (i >= n) {
126
- ans.push_back (t);
125
+ ans.emplace_back (t);
127
126
return;
128
127
}
129
128
for (int j = 0; j < n; ++j) {
130
- if (vis[ j] ) {
131
- continue;
129
+ if (!vis[ j] ) {
130
+ vis[ j] = true;
131
+ t[ i] = S[ j] ;
132
+ dfs(i + 1);
133
+ vis[ j] = false;
132
134
}
133
- vis[ j] = true;
134
- t.push_back(S[ j] );
135
- dfs(i + 1);
136
- t.pop_back();
137
- vis[ j] = false;
138
135
}
139
136
};
140
137
dfs(0);
141
138
return ans;
142
139
}
143
140
};
141
+
144
142
```
145
143
146
144
#### Go
147
145
148
146
```go
149
147
func permutation(S string) (ans []string) {
150
- t := []byte{}
151
- vis := make([]bool, len(S))
148
+ t := []byte(S)
149
+ n := len(t)
150
+ vis := make([]bool, n)
152
151
var dfs func(int)
153
152
dfs = func(i int) {
154
- if i >= len(S) {
153
+ if i >= n {
155
154
ans = append(ans, string(t))
156
155
return
157
156
}
158
157
for j := range S {
159
- if vis[j] {
160
- continue
158
+ if !vis[j] {
159
+ vis[j] = true
160
+ t[i] = S[j]
161
+ dfs(i + 1)
162
+ vis[j] = false
161
163
}
162
- vis[j] = true
163
- t = append(t, S[j])
164
- dfs(i + 1)
165
- t = t[:len(t)-1]
166
- vis[j] = false
167
164
}
168
165
}
169
166
dfs(0)
@@ -178,7 +175,7 @@ function permutation(S: string): string[] {
178
175
const n = S .length ;
179
176
const vis: boolean [] = Array (n ).fill (false );
180
177
const ans: string [] = [];
181
- const t: string [] = [] ;
178
+ const t: string [] = Array ( n ). fill ( ' ' ) ;
182
179
const dfs = (i : number ) => {
183
180
if (i >= n ) {
184
181
ans .push (t .join (' ' ));
@@ -189,9 +186,8 @@ function permutation(S: string): string[] {
189
186
continue ;
190
187
}
191
188
vis [j ] = true ;
192
- t . push ( S [j ]) ;
189
+ t [ i ] = S [j ];
193
190
dfs (i + 1 );
194
- t .pop ();
195
191
vis [j ] = false ;
196
192
}
197
193
};
@@ -211,7 +207,7 @@ var permutation = function (S) {
211
207
const n = S .length ;
212
208
const vis = Array (n).fill (false );
213
209
const ans = [];
214
- const t = [] ;
210
+ const t = Array (n). fill ( ' ' ) ;
215
211
const dfs = i => {
216
212
if (i >= n) {
217
213
ans .push (t .join (' ' ));
@@ -222,9 +218,8 @@ var permutation = function (S) {
222
218
continue ;
223
219
}
224
220
vis[j] = true ;
225
- t . push ( S [j]) ;
221
+ t[i] = S [j];
226
222
dfs (i + 1 );
227
- t .pop ();
228
223
vis[j] = false ;
229
224
}
230
225
};
@@ -237,33 +232,30 @@ var permutation = function (S) {
237
232
238
233
``` swift
239
234
class Solution {
240
- private var s: [Character ] = []
241
- private var vis: [Bool ] = Array (repeating : false , count : 128 )
242
- private var ans: [String ] = []
243
- private var t: String = " "
244
-
245
235
func permutation (_ S : String ) -> [String ] {
246
- s = Array (S)
247
- dfs (0 )
248
- return ans
249
- }
250
-
251
- private func dfs (_ i : Int ) {
252
- if i == s.count {
253
- ans.append (t)
254
- return
255
- }
256
- for c in s {
257
- let index = Int (c.asciiValue ! )
258
- if vis[index] {
259
- continue
236
+ var ans: [String ] = []
237
+ let s = Array (S)
238
+ var t = s
239
+ var vis = Array (repeating : false , count : s.count )
240
+ let n = s.count
241
+
242
+ func dfs (_ i : Int ) {
243
+ if i >= n {
244
+ ans.append (String (t))
245
+ return
246
+ }
247
+ for j in 0 ..< n {
248
+ if ! vis[j] {
249
+ vis[j] = true
250
+ t[i] = s[j]
251
+ dfs (i + 1 )
252
+ vis[j] = false
253
+ }
260
254
}
261
- vis[index] = true
262
- t.append (c)
263
- dfs (i + 1 )
264
- t.removeLast ()
265
- vis[index] = false
266
255
}
256
+
257
+ dfs (0 )
258
+ return ans
267
259
}
268
260
}
269
261
```
0 commit comments