Skip to content

Commit d467151

Browse files
authored
feat: add solutions to lcci problems: No.08.06~08.09 (#4068)
1 parent f3bd387 commit d467151

24 files changed

+482
-568
lines changed

lcci/08.06.Hanota/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Solution {
9191
class Solution {
9292
public:
9393
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
94-
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
94+
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
9595
if (n == 1) {
9696
c.push_back(a.back());
9797
a.pop_back();

lcci/08.06.Hanota/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Solution {
9898
class Solution {
9999
public:
100100
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
101-
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
101+
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
102102
if (n == 1) {
103103
c.push_back(a.back());
104104
a.pop_back();

lcci/08.06.Hanota/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public:
33
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
4-
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
4+
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
55
if (n == 1) {
66
c.push_back(a.back());
77
a.pop_back();
@@ -14,4 +14,4 @@ class Solution {
1414
};
1515
dfs(A.size(), A, B, C);
1616
}
17-
};
17+
};

lcci/08.07.Permutation I/README.md

+65-73
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
4545

4646
### 方法一:DFS(回溯)
4747

48-
我们设计一个函数 $dfs(i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
48+
我们设计一个函数 $\textit{dfs}(i)$ 表示已经填完了前 $i$ 个位置,现在需要填第 $i+1$ 个位置。枚举所有可能的字符,如果这个字符没有被填过,就填入这个字符,然后继续填下一个位置,直到填完所有的位置。
4949

5050
时间复杂度 $O(n \times n!)$,其中 $n$ 是字符串的长度。一共有 $n!$ 个排列,每个排列需要 $O(n)$ 的时间来构造。
5151

@@ -57,22 +57,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
5757
class Solution:
5858
def permutation(self, S: str) -> List[str]:
5959
def dfs(i: int):
60-
if i == n:
60+
if i >= n:
6161
ans.append("".join(t))
6262
return
6363
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
7169

70+
ans = []
7271
n = len(S)
7372
vis = [False] * n
74-
ans = []
75-
t = []
73+
t = list(S)
7674
dfs(0)
7775
return ans
7876
```
@@ -82,30 +80,31 @@ class Solution:
8280
```java
8381
class Solution {
8482
private char[] s;
85-
private boolean[] vis = new boolean['z' + 1];
83+
private char[] t;
84+
private boolean[] vis;
8685
private List<String> ans = new ArrayList<>();
87-
private StringBuilder t = new StringBuilder();
8886

8987
public String[] permutation(String S) {
9088
s = S.toCharArray();
89+
int n = s.length;
90+
vis = new boolean[n];
91+
t = new char[n];
9192
dfs(0);
9293
return ans.toArray(new String[0]);
9394
}
9495

9596
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));
9899
return;
99100
}
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;
103107
}
104-
vis[c] = true;
105-
t.append(c);
106-
dfs(i + 1);
107-
t.deleteCharAt(t.length() - 1);
108-
vis[c] = false;
109108
}
110109
}
111110
}
@@ -119,51 +118,49 @@ public:
119118
vector<string> permutation(string S) {
120119
int n = S.size();
121120
vector<bool> vis(n);
121+
string t = S;
122122
vector<string> ans;
123-
string t;
124-
function<void(int)> dfs = [&](int i) {
123+
auto dfs = [&](this auto&& dfs, int i) {
125124
if (i >= n) {
126-
ans.push_back(t);
125+
ans.emplace_back(t);
127126
return;
128127
}
129128
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;
132134
}
133-
vis[j] = true;
134-
t.push_back(S[j]);
135-
dfs(i + 1);
136-
t.pop_back();
137-
vis[j] = false;
138135
}
139136
};
140137
dfs(0);
141138
return ans;
142139
}
143140
};
141+
144142
```
145143
146144
#### Go
147145
148146
```go
149147
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)
152151
var dfs func(int)
153152
dfs = func(i int) {
154-
if i >= len(S) {
153+
if i >= n {
155154
ans = append(ans, string(t))
156155
return
157156
}
158157
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
161163
}
162-
vis[j] = true
163-
t = append(t, S[j])
164-
dfs(i + 1)
165-
t = t[:len(t)-1]
166-
vis[j] = false
167164
}
168165
}
169166
dfs(0)
@@ -178,7 +175,7 @@ function permutation(S: string): string[] {
178175
const n = S.length;
179176
const vis: boolean[] = Array(n).fill(false);
180177
const ans: string[] = [];
181-
const t: string[] = [];
178+
const t: string[] = Array(n).fill('');
182179
const dfs = (i: number) => {
183180
if (i >= n) {
184181
ans.push(t.join(''));
@@ -189,9 +186,8 @@ function permutation(S: string): string[] {
189186
continue;
190187
}
191188
vis[j] = true;
192-
t.push(S[j]);
189+
t[i] = S[j];
193190
dfs(i + 1);
194-
t.pop();
195191
vis[j] = false;
196192
}
197193
};
@@ -211,7 +207,7 @@ var permutation = function (S) {
211207
const n = S.length;
212208
const vis = Array(n).fill(false);
213209
const ans = [];
214-
const t = [];
210+
const t = Array(n).fill('');
215211
const dfs = i => {
216212
if (i >= n) {
217213
ans.push(t.join(''));
@@ -222,9 +218,8 @@ var permutation = function (S) {
222218
continue;
223219
}
224220
vis[j] = true;
225-
t.push(S[j]);
221+
t[i] = S[j];
226222
dfs(i + 1);
227-
t.pop();
228223
vis[j] = false;
229224
}
230225
};
@@ -237,33 +232,30 @@ var permutation = function (S) {
237232

238233
```swift
239234
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-
245235
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+
}
260254
}
261-
vis[index] = true
262-
t.append(c)
263-
dfs(i + 1)
264-
t.removeLast()
265-
vis[index] = false
266255
}
256+
257+
dfs(0)
258+
return ans
267259
}
268260
}
269261
```

0 commit comments

Comments
 (0)