Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lcci problems: No.08.06~08.09 #4068

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lcci/08.06.Hanota/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Solution {
class Solution {
public:
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
if (n == 1) {
c.push_back(a.back());
a.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion lcci/08.06.Hanota/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Solution {
class Solution {
public:
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
if (n == 1) {
c.push_back(a.back());
a.pop_back();
Expand Down
4 changes: 2 additions & 2 deletions lcci/08.06.Hanota/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
public:
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
function<void(int, vector<int>&, vector<int>&, vector<int>&)> dfs = [&](int n, vector<int>& a, vector<int>& b, vector<int>& c) {
auto dfs = [&](this auto&& dfs, int n, vector<int>& a, vector<int>& b, vector<int>& c) {
if (n == 1) {
c.push_back(a.back());
a.pop_back();
Expand All @@ -14,4 +14,4 @@ class Solution {
};
dfs(A.size(), A, B, C);
}
};
};
138 changes: 65 additions & 73 deletions lcci/08.07.Permutation I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I

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

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

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

Expand All @@ -57,22 +57,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.07.Permutation%20I
class Solution:
def permutation(self, S: str) -> List[str]:
def dfs(i: int):
if i == n:
if i >= n:
ans.append("".join(t))
return
for j, c in enumerate(S):
if vis[j]:
continue
vis[j] = True
t.append(c)
dfs(i + 1)
t.pop()
vis[j] = False
if not vis[j]:
vis[j] = True
t[i] = c
dfs(i + 1)
vis[j] = False

ans = []
n = len(S)
vis = [False] * n
ans = []
t = []
t = list(S)
dfs(0)
return ans
```
Expand All @@ -82,30 +80,31 @@ class Solution:
```java
class Solution {
private char[] s;
private boolean[] vis = new boolean['z' + 1];
private char[] t;
private boolean[] vis;
private List<String> ans = new ArrayList<>();
private StringBuilder t = new StringBuilder();

public String[] permutation(String S) {
s = S.toCharArray();
int n = s.length;
vis = new boolean[n];
t = new char[n];
dfs(0);
return ans.toArray(new String[0]);
}

private void dfs(int i) {
if (i == s.length) {
ans.add(t.toString());
if (i >= s.length) {
ans.add(new String(t));
return;
}
for (char c : s) {
if (vis[c]) {
continue;
for (int j = 0; j < s.length; ++j) {
if (!vis[j]) {
vis[j] = true;
t[i] = s[j];
dfs(i + 1);
vis[j] = false;
}
vis[c] = true;
t.append(c);
dfs(i + 1);
t.deleteCharAt(t.length() - 1);
vis[c] = false;
}
}
}
Expand All @@ -119,51 +118,49 @@ public:
vector<string> permutation(string S) {
int n = S.size();
vector<bool> vis(n);
string t = S;
vector<string> ans;
string t;
function<void(int)> dfs = [&](int i) {
auto dfs = [&](this auto&& dfs, int i) {
if (i >= n) {
ans.push_back(t);
ans.emplace_back(t);
return;
}
for (int j = 0; j < n; ++j) {
if (vis[j]) {
continue;
if (!vis[j]) {
vis[j] = true;
t[i] = S[j];
dfs(i + 1);
vis[j] = false;
}
vis[j] = true;
t.push_back(S[j]);
dfs(i + 1);
t.pop_back();
vis[j] = false;
}
};
dfs(0);
return ans;
}
};

```

#### Go

```go
func permutation(S string) (ans []string) {
t := []byte{}
vis := make([]bool, len(S))
t := []byte(S)
n := len(t)
vis := make([]bool, n)
var dfs func(int)
dfs = func(i int) {
if i >= len(S) {
if i >= n {
ans = append(ans, string(t))
return
}
for j := range S {
if vis[j] {
continue
if !vis[j] {
vis[j] = true
t[i] = S[j]
dfs(i + 1)
vis[j] = false
}
vis[j] = true
t = append(t, S[j])
dfs(i + 1)
t = t[:len(t)-1]
vis[j] = false
}
}
dfs(0)
Expand All @@ -178,7 +175,7 @@ function permutation(S: string): string[] {
const n = S.length;
const vis: boolean[] = Array(n).fill(false);
const ans: string[] = [];
const t: string[] = [];
const t: string[] = Array(n).fill('');
const dfs = (i: number) => {
if (i >= n) {
ans.push(t.join(''));
Expand All @@ -189,9 +186,8 @@ function permutation(S: string): string[] {
continue;
}
vis[j] = true;
t.push(S[j]);
t[i] = S[j];
dfs(i + 1);
t.pop();
vis[j] = false;
}
};
Expand All @@ -211,7 +207,7 @@ var permutation = function (S) {
const n = S.length;
const vis = Array(n).fill(false);
const ans = [];
const t = [];
const t = Array(n).fill('');
const dfs = i => {
if (i >= n) {
ans.push(t.join(''));
Expand All @@ -222,9 +218,8 @@ var permutation = function (S) {
continue;
}
vis[j] = true;
t.push(S[j]);
t[i] = S[j];
dfs(i + 1);
t.pop();
vis[j] = false;
}
};
Expand All @@ -237,33 +232,30 @@ var permutation = function (S) {

```swift
class Solution {
private var s: [Character] = []
private var vis: [Bool] = Array(repeating: false, count: 128)
private var ans: [String] = []
private var t: String = ""

func permutation(_ S: String) -> [String] {
s = Array(S)
dfs(0)
return ans
}

private func dfs(_ i: Int) {
if i == s.count {
ans.append(t)
return
}
for c in s {
let index = Int(c.asciiValue!)
if vis[index] {
continue
var ans: [String] = []
let s = Array(S)
var t = s
var vis = Array(repeating: false, count: s.count)
let n = s.count

func dfs(_ i: Int) {
if i >= n {
ans.append(String(t))
return
}
for j in 0..<n {
if !vis[j] {
vis[j] = true
t[i] = s[j]
dfs(i + 1)
vis[j] = false
}
}
vis[index] = true
t.append(c)
dfs(i + 1)
t.removeLast()
vis[index] = false
}

dfs(0)
return ans
}
}
```
Expand Down
Loading