Skip to content

Commit 5ae382e

Browse files
committed
feat: add solutions to lcci problem: No.08.07
No.08.07.Permutation I
1 parent 278e7b9 commit 5ae382e

File tree

6 files changed

+310
-3
lines changed

6 files changed

+310
-3
lines changed

lcci/08.07.Permutation I/README.md

+107-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<!-- 这里可写通用的实现逻辑 -->
3434

35-
回溯法
35+
**方法一:回溯**
3636

3737
<!-- tabs:start -->
3838

@@ -41,15 +41,59 @@
4141
<!-- 这里可写当前语言的特殊实现逻辑 -->
4242

4343
```python
44-
44+
class Solution:
45+
def permutation(self, S: str) -> List[str]:
46+
def dfs(u, t):
47+
if u == n:
48+
ans.append(''.join(t))
49+
return
50+
for i in range(n):
51+
if vis[i]:
52+
continue
53+
vis[i] = True
54+
t.append(S[i])
55+
dfs(u + 1, t)
56+
t.pop()
57+
vis[i] = False
58+
59+
n = len(S)
60+
vis = [False] * n
61+
ans = []
62+
dfs(0, [])
63+
return ans
4564
```
4665

4766
### **Java**
4867

4968
<!-- 这里可写当前语言的特殊实现逻辑 -->
5069

5170
```java
71+
class Solution {
72+
public String[] permutation(String S) {
73+
Set<Character> vis = new HashSet<>();
74+
List<String> ans = new ArrayList<>();
75+
StringBuilder t = new StringBuilder();
76+
dfs(0, S, t, ans, vis);
77+
return ans.toArray(new String[0]);
78+
}
5279

80+
private void dfs(int u, String S, StringBuilder t, List<String> ans, Set<Character> vis) {
81+
if (u == S.length()) {
82+
ans.add(t.toString());
83+
return;
84+
}
85+
for (char c : S.toCharArray()) {
86+
if (vis.contains(c)) {
87+
continue;
88+
}
89+
vis.add(c);
90+
t.append(c);
91+
dfs(u + 1, S, t, ans, vis);
92+
t.deleteCharAt(t.length() - 1);
93+
vis.remove(c);
94+
}
95+
}
96+
}
5397
```
5498

5599
### **JavaSript**
@@ -86,6 +130,67 @@ function dfs(arr, depth, prev, record, res) {
86130
}
87131
```
88132

133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
vector<string> permutation(string S) {
139+
unordered_set<char> vis;
140+
vector<string> ans;
141+
string t = "";
142+
dfs(0, S, t, ans, vis);
143+
return ans;
144+
}
145+
146+
void dfs(int u, string& S, string& t, vector<string>& ans, unordered_set<char>& vis) {
147+
if (u == S.size())
148+
{
149+
ans.push_back(t);
150+
return;
151+
}
152+
for (char& c : S)
153+
{
154+
if (vis.count(c)) continue;
155+
vis.insert(c);
156+
t.push_back(c);
157+
dfs(u + 1, S, t, ans, vis);
158+
vis.erase(c);
159+
t.pop_back();
160+
}
161+
}
162+
};
163+
```
164+
165+
### **Go**
166+
167+
```go
168+
func permutation(S string) []string {
169+
vis := make(map[byte]bool)
170+
var ans []string
171+
var t []byte
172+
var dfs func(u int, t []byte)
173+
dfs = func(u int, t []byte) {
174+
if u == len(S) {
175+
ans = append(ans, string(t))
176+
return
177+
}
178+
for i := range S {
179+
if vis[S[i]] {
180+
continue
181+
}
182+
vis[S[i]] = true
183+
t = append(t, S[i])
184+
dfs(u+1, t)
185+
vis[S[i]] = false
186+
t = t[:len(t)-1]
187+
}
188+
}
189+
dfs(0, t)
190+
return ans
191+
}
192+
```
193+
89194
### **...**
90195

91196
```

lcci/08.07.Permutation I/README_EN.md

+106-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,57 @@ Backtracking
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def permutation(self, S: str) -> List[str]:
47+
def dfs(u, t):
48+
if u == n:
49+
ans.append(''.join(t))
50+
return
51+
for i in range(n):
52+
if vis[i]:
53+
continue
54+
vis[i] = True
55+
t.append(S[i])
56+
dfs(u + 1, t)
57+
t.pop()
58+
vis[i] = False
59+
60+
n = len(S)
61+
vis = [False] * n
62+
ans = []
63+
dfs(0, [])
64+
return ans
4665
```
4766

4867
### **Java**
4968

5069
```java
70+
class Solution {
71+
public String[] permutation(String S) {
72+
Set<Character> vis = new HashSet<>();
73+
List<String> ans = new ArrayList<>();
74+
StringBuilder t = new StringBuilder();
75+
dfs(0, S, t, ans, vis);
76+
return ans.toArray(new String[0]);
77+
}
5178

79+
private void dfs(int u, String S, StringBuilder t, List<String> ans, Set<Character> vis) {
80+
if (u == S.length()) {
81+
ans.add(t.toString());
82+
return;
83+
}
84+
for (char c : S.toCharArray()) {
85+
if (vis.contains(c)) {
86+
continue;
87+
}
88+
vis.add(c);
89+
t.append(c);
90+
dfs(u + 1, S, t, ans, vis);
91+
t.deleteCharAt(t.length() - 1);
92+
vis.remove(c);
93+
}
94+
}
95+
}
5296
```
5397

5498
### **JavaSript**
@@ -85,6 +129,67 @@ function dfs(arr, depth, prev, record, res) {
85129
}
86130
```
87131

132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
vector<string> permutation(string S) {
138+
unordered_set<char> vis;
139+
vector<string> ans;
140+
string t = "";
141+
dfs(0, S, t, ans, vis);
142+
return ans;
143+
}
144+
145+
void dfs(int u, string& S, string& t, vector<string>& ans, unordered_set<char>& vis) {
146+
if (u == S.size())
147+
{
148+
ans.push_back(t);
149+
return;
150+
}
151+
for (char& c : S)
152+
{
153+
if (vis.count(c)) continue;
154+
vis.insert(c);
155+
t.push_back(c);
156+
dfs(u + 1, S, t, ans, vis);
157+
vis.erase(c);
158+
t.pop_back();
159+
}
160+
}
161+
};
162+
```
163+
164+
### **Go**
165+
166+
```go
167+
func permutation(S string) []string {
168+
vis := make(map[byte]bool)
169+
var ans []string
170+
var t []byte
171+
var dfs func(u int, t []byte)
172+
dfs = func(u int, t []byte) {
173+
if u == len(S) {
174+
ans = append(ans, string(t))
175+
return
176+
}
177+
for i := range S {
178+
if vis[S[i]] {
179+
continue
180+
}
181+
vis[S[i]] = true
182+
t = append(t, S[i])
183+
dfs(u+1, t)
184+
vis[S[i]] = false
185+
t = t[:len(t)-1]
186+
}
187+
}
188+
dfs(0, t)
189+
return ans
190+
}
191+
```
192+
88193
### **...**
89194

90195
```

lcci/08.07.Permutation I/Solution.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<string> permutation(string S) {
4+
unordered_set<char> vis;
5+
vector<string> ans;
6+
string t = "";
7+
dfs(0, S, t, ans, vis);
8+
return ans;
9+
}
10+
11+
void dfs(int u, string& S, string& t, vector<string>& ans, unordered_set<char>& vis) {
12+
if (u == S.size())
13+
{
14+
ans.push_back(t);
15+
return;
16+
}
17+
for (char& c : S)
18+
{
19+
if (vis.count(c)) continue;
20+
vis.insert(c);
21+
t.push_back(c);
22+
dfs(u + 1, S, t, ans, vis);
23+
vis.erase(c);
24+
t.pop_back();
25+
}
26+
}
27+
};

lcci/08.07.Permutation I/Solution.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func permutation(S string) []string {
2+
vis := make(map[byte]bool)
3+
var ans []string
4+
var t []byte
5+
var dfs func(u int, t []byte)
6+
dfs = func(u int, t []byte) {
7+
if u == len(S) {
8+
ans = append(ans, string(t))
9+
return
10+
}
11+
for i := range S {
12+
if vis[S[i]] {
13+
continue
14+
}
15+
vis[S[i]] = true
16+
t = append(t, S[i])
17+
dfs(u+1, t)
18+
vis[S[i]] = false
19+
t = t[:len(t)-1]
20+
}
21+
}
22+
dfs(0, t)
23+
return ans
24+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public String[] permutation(String S) {
3+
Set<Character> vis = new HashSet<>();
4+
List<String> ans = new ArrayList<>();
5+
StringBuilder t = new StringBuilder();
6+
dfs(0, S, t, ans, vis);
7+
return ans.toArray(new String[0]);
8+
}
9+
10+
private void dfs(int u, String S, StringBuilder t, List<String> ans, Set<Character> vis) {
11+
if (u == S.length()) {
12+
ans.add(t.toString());
13+
return;
14+
}
15+
for (char c : S.toCharArray()) {
16+
if (vis.contains(c)) {
17+
continue;
18+
}
19+
vis.add(c);
20+
t.append(c);
21+
dfs(u + 1, S, t, ans, vis);
22+
t.deleteCharAt(t.length() - 1);
23+
vis.remove(c);
24+
}
25+
}
26+
}

lcci/08.07.Permutation I/Solution.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def permutation(self, S: str) -> List[str]:
3+
def dfs(u, t):
4+
if u == n:
5+
ans.append(''.join(t))
6+
return
7+
for i in range(n):
8+
if vis[i]:
9+
continue
10+
vis[i] = True
11+
t.append(S[i])
12+
dfs(u + 1, t)
13+
t.pop()
14+
vis[i] = False
15+
16+
n = len(S)
17+
vis = [False] * n
18+
ans = []
19+
dfs(0, [])
20+
return ans

0 commit comments

Comments
 (0)