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 89dd182

Browse files
feat: add swift implementation to lcci problem: No.08.08 (doocs#2694)
1 parent c4e3b56 commit 89dd182

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

‎lcci/08.08.Permutation II/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,42 @@ var permutation = function (S) {
219219
};
220220
```
221221

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+
222258
<!-- tabs:end -->
223259

224260
<!-- end -->

‎lcci/08.08.Permutation II/README_EN.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,42 @@ var permutation = function (S) {
225225
};
226226
```
227227

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+
228264
<!-- tabs:end -->
229265

230266
<!-- end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
(0)

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