File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed
lcci/08.08.Permutation II Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,42 @@ var permutation = function (S) {
219
219
};
220
220
```
221
221
222
+ ``` swift
223
+ class Solution {
224
+ private var n: Int = 0
225
+ private var cs: [Character ] = []
226
+ private var ans: [String ] = []
227
+ private var vis: [Bool ] = []
228
+ private var t: String = " "
229
+
230
+ func permutation (_ S : String ) -> [String ] {
231
+ cs = Array (S)
232
+ n = cs.count
233
+ cs.sort ()
234
+ vis = Array (repeating : false , count : n)
235
+ dfs (0 )
236
+ return ans
237
+ }
238
+
239
+ private func dfs (_ i : Int ) {
240
+ if i == n {
241
+ ans.append (t)
242
+ return
243
+ }
244
+ for j in 0 ..< n {
245
+ if vis[j] || (j > 0 && ! vis[j - 1 ] && cs[j] == cs[j - 1 ]) {
246
+ continue
247
+ }
248
+ vis[j] = true
249
+ t.append (cs[j])
250
+ dfs (i + 1 )
251
+ t.removeLast ()
252
+ vis[j] = false
253
+ }
254
+ }
255
+ }
256
+ ```
257
+
222
258
<!-- tabs: end -->
223
259
224
260
<!-- end -->
Original file line number Diff line number Diff line change @@ -225,6 +225,42 @@ var permutation = function (S) {
225
225
};
226
226
```
227
227
228
+ ``` swift
229
+ class Solution {
230
+ private var n: Int = 0
231
+ private var cs: [Character ] = []
232
+ private var ans: [String ] = []
233
+ private var vis: [Bool ] = []
234
+ private var t: String = " "
235
+
236
+ func permutation (_ S : String ) -> [String ] {
237
+ cs = Array (S)
238
+ n = cs.count
239
+ cs.sort ()
240
+ vis = Array (repeating : false , count : n)
241
+ dfs (0 )
242
+ return ans
243
+ }
244
+
245
+ private func dfs (_ i : Int ) {
246
+ if i == n {
247
+ ans.append (t)
248
+ return
249
+ }
250
+ for j in 0 ..< n {
251
+ if vis[j] || (j > 0 && ! vis[j - 1 ] && cs[j] == cs[j - 1 ]) {
252
+ continue
253
+ }
254
+ vis[j] = true
255
+ t.append (cs[j])
256
+ dfs (i + 1 )
257
+ t.removeLast ()
258
+ vis[j] = false
259
+ }
260
+ }
261
+ }
262
+ ```
263
+
228
264
<!-- tabs: end -->
229
265
230
266
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private var n : Int = 0
3
+ private var cs : [ Character ] = [ ]
4
+ private var ans : [ String ] = [ ]
5
+ private var vis : [ Bool ] = [ ]
6
+ private var t : String = " "
7
+
8
+ func permutation( _ S: String ) -> [ String ] {
9
+ cs = Array ( S)
10
+ n = cs. count
11
+ cs. sort ( )
12
+ vis = Array ( repeating: false , count: n)
13
+ dfs ( 0 )
14
+ return ans
15
+ }
16
+
17
+ private func dfs( _ i: Int ) {
18
+ if i == n {
19
+ ans. append ( t)
20
+ return
21
+ }
22
+ for j in 0 ..< n {
23
+ if vis [ j] || ( j > 0 && !vis[ j - 1 ] && cs [ j] == cs [ j - 1 ] ) {
24
+ continue
25
+ }
26
+ vis [ j] = true
27
+ t. append ( cs [ j] )
28
+ dfs ( i + 1 )
29
+ t. removeLast ( )
30
+ vis [ j] = false
31
+ }
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments