From 87f1494d71e7a7aa5791de4536f2f8c0688b9a62 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 15 Feb 2025 17:29:38 +0800 Subject: [PATCH] feat: add solutions to lcci problems: No.08.06~08.09 --- lcci/08.06.Hanota/README.md | 2 +- lcci/08.06.Hanota/README_EN.md | 2 +- lcci/08.06.Hanota/Solution.cpp | 4 +- lcci/08.07.Permutation I/README.md | 138 +++++++-------- lcci/08.07.Permutation I/README_EN.md | 137 +++++++-------- lcci/08.07.Permutation I/Solution.cpp | 20 +-- lcci/08.07.Permutation I/Solution.go | 21 ++- lcci/08.07.Permutation I/Solution.java | 27 +-- lcci/08.07.Permutation I/Solution.js | 5 +- lcci/08.07.Permutation I/Solution.py | 18 +- lcci/08.07.Permutation I/Solution.swift | 43 +++-- lcci/08.07.Permutation I/Solution.ts | 5 +- lcci/08.08.Permutation II/README.md | 205 ++++++++++------------ lcci/08.08.Permutation II/README_EN.md | 211 +++++++++++------------ lcci/08.08.Permutation II/Solution.cpp | 29 ++-- lcci/08.08.Permutation II/Solution.go | 27 ++- lcci/08.08.Permutation II/Solution.java | 34 ++-- lcci/08.08.Permutation II/Solution.js | 22 ++- lcci/08.08.Permutation II/Solution.py | 21 ++- lcci/08.08.Permutation II/Solution.swift | 46 +++-- lcci/08.08.Permutation II/Solution.ts | 22 ++- lcci/08.09.Bracket/README.md | 3 +- lcci/08.09.Bracket/README_EN.md | 3 +- lcci/08.09.Bracket/Solution.cpp | 5 +- 24 files changed, 482 insertions(+), 568 deletions(-) diff --git a/lcci/08.06.Hanota/README.md b/lcci/08.06.Hanota/README.md index 04efe8940d384..b5dc3d3966adc 100644 --- a/lcci/08.06.Hanota/README.md +++ b/lcci/08.06.Hanota/README.md @@ -91,7 +91,7 @@ class Solution { class Solution { public: void hanota(vector& A, vector& B, vector& C) { - function&, vector&, vector&)> dfs = [&](int n, vector& a, vector& b, vector& c) { + auto dfs = [&](this auto&& dfs, int n, vector& a, vector& b, vector& c) { if (n == 1) { c.push_back(a.back()); a.pop_back(); diff --git a/lcci/08.06.Hanota/README_EN.md b/lcci/08.06.Hanota/README_EN.md index 6d4243950efb0..3f52664c584e4 100644 --- a/lcci/08.06.Hanota/README_EN.md +++ b/lcci/08.06.Hanota/README_EN.md @@ -98,7 +98,7 @@ class Solution { class Solution { public: void hanota(vector& A, vector& B, vector& C) { - function&, vector&, vector&)> dfs = [&](int n, vector& a, vector& b, vector& c) { + auto dfs = [&](this auto&& dfs, int n, vector& a, vector& b, vector& c) { if (n == 1) { c.push_back(a.back()); a.pop_back(); diff --git a/lcci/08.06.Hanota/Solution.cpp b/lcci/08.06.Hanota/Solution.cpp index 7cf7ad9647324..3b5b8d483ce8b 100644 --- a/lcci/08.06.Hanota/Solution.cpp +++ b/lcci/08.06.Hanota/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: void hanota(vector& A, vector& B, vector& C) { - function&, vector&, vector&)> dfs = [&](int n, vector& a, vector& b, vector& c) { + auto dfs = [&](this auto&& dfs, int n, vector& a, vector& b, vector& c) { if (n == 1) { c.push_back(a.back()); a.pop_back(); @@ -14,4 +14,4 @@ class Solution { }; dfs(A.size(), A, B, C); } -}; \ No newline at end of file +}; diff --git a/lcci/08.07.Permutation I/README.md b/lcci/08.07.Permutation I/README.md index 34a783fde5bac..9d3e78c2da0d4 100644 --- a/lcci/08.07.Permutation I/README.md +++ b/lcci/08.07.Permutation I/README.md @@ -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)$ 的时间来构造。 @@ -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 ``` @@ -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 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; } } } @@ -119,51 +118,49 @@ public: vector permutation(string S) { int n = S.size(); vector vis(n); + string t = S; vector ans; - string t; - function 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) @@ -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('')); @@ -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; } }; @@ -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('')); @@ -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; } }; @@ -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.. 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 ``` @@ -88,30 +86,31 @@ class Solution: ```java class Solution { private char[] s; - private boolean[] vis = new boolean['z' + 1]; + private char[] t; + private boolean[] vis; private List 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; } } } @@ -125,22 +124,20 @@ public: vector permutation(string S) { int n = S.size(); vector vis(n); + string t = S; vector ans; - string t; - function 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); @@ -153,23 +150,22 @@ public: ```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) @@ -184,7 +180,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('')); @@ -195,9 +191,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; } }; @@ -217,7 +212,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('')); @@ -228,9 +223,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; } }; @@ -243,33 +237,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.. permutation(string S) { int n = S.size(); vector vis(n); + string t = S; vector ans; - string t; - function 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; } -}; \ No newline at end of file +}; diff --git a/lcci/08.07.Permutation I/Solution.go b/lcci/08.07.Permutation I/Solution.go index 20d5c55fded5f..1f84fb692481e 100644 --- a/lcci/08.07.Permutation I/Solution.go +++ b/lcci/08.07.Permutation I/Solution.go @@ -1,23 +1,22 @@ 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) return -} \ No newline at end of file +} diff --git a/lcci/08.07.Permutation I/Solution.java b/lcci/08.07.Permutation I/Solution.java index 896ed99f9d1ea..f65456f945759 100644 --- a/lcci/08.07.Permutation I/Solution.java +++ b/lcci/08.07.Permutation I/Solution.java @@ -1,29 +1,30 @@ class Solution { private char[] s; - private boolean[] vis = new boolean['z' + 1]; + private char[] t; + private boolean[] vis; private List 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; } } -} \ No newline at end of file +} diff --git a/lcci/08.07.Permutation I/Solution.js b/lcci/08.07.Permutation I/Solution.js index f1dcb5d98f69f..c18ebd79c017d 100644 --- a/lcci/08.07.Permutation I/Solution.js +++ b/lcci/08.07.Permutation I/Solution.js @@ -6,7 +6,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('')); @@ -17,9 +17,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; } }; diff --git a/lcci/08.07.Permutation I/Solution.py b/lcci/08.07.Permutation I/Solution.py index 537c2f0484bb7..7e64e799f8691 100644 --- a/lcci/08.07.Permutation I/Solution.py +++ b/lcci/08.07.Permutation I/Solution.py @@ -1,21 +1,19 @@ 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 diff --git a/lcci/08.07.Permutation I/Solution.swift b/lcci/08.07.Permutation I/Solution.swift index 48803e420ab09..33a987fc012d9 100644 --- a/lcci/08.07.Permutation I/Solution.swift +++ b/lcci/08.07.Permutation I/Solution.swift @@ -1,30 +1,27 @@ 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 - } + var ans: [String] = [] + let s = Array(S) + var t = s + var vis = Array(repeating: false, count: s.count) + let n = s.count - 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 + func dfs(_ i: Int) { + if i >= n { + ans.append(String(t)) + return + } + for j in 0.. { if (i >= n) { ans.push(t.join('')); @@ -13,9 +13,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; } }; diff --git a/lcci/08.08.Permutation II/README.md b/lcci/08.08.Permutation II/README.md index 90b72d44fb2f1..936fb1b9d90cf 100644 --- a/lcci/08.08.Permutation II/README.md +++ b/lcci/08.08.Permutation II/README.md @@ -39,12 +39,12 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.08.Permutation%20I 我们可以先对字符串按照字符进行排序,这样就可以将重复的字符放在一起,方便我们进行去重。 -然后,我们设计一个函数 $dfs(i)$,表示当前需要填写第 $i$ 个位置的字符。函数的具体实现如下: +然后,我们设计一个函数 $\textit{dfs}(i)$,表示当前需要填写第 $i$ 个位置的字符。函数的具体实现如下: - 如果 $i = n$,说明我们已经填写完毕,将当前排列加入答案数组中,然后返回。 -- 否则,我们枚举第 $i$ 个位置的字符 $s[j]$,其中 $j$ 的范围是 $[0, n - 1]$。我们需要保证 $s[j]$ 没有被使用过,并且与前面枚举的字符不同,这样才能保证当前排列不重复。如果满足条件,我们就可以填写 $s[j]$,并继续递归地填写下一个位置,即调用 $dfs(i + 1)$。在递归调用结束后,我们需要将 $s[j]$ 标记为未使用,以便于进行后面的枚举。 +- 否则,我们枚举第 $i$ 个位置的字符 $\textit{s}[j]$,其中 $j$ 的范围是 $[0, n - 1]$。我们需要保证 $\textit{s}[j]$ 没有被使用过,并且与前面枚举的字符不同,这样才能保证当前排列不重复。如果满足条件,我们就可以填写 $\textit{s}[j]$,并继续递归地填写下一个位置,即调用 $\textit{dfs}(i + 1)$。在递归调用结束后,我们需要将 $\textit{s}[j]$ 标记为未使用,以便于进行后面的枚举。 -在主函数中,我们首先对字符串进行排序,然后调用 $dfs(0)$,即从第 $0$ 个位置开始填写,最终返回答案数组即可。 +在主函数中,我们首先对字符串进行排序,然后调用 $\textit{dfs}(0)$,即从第 $0$ 个位置开始填写,最终返回答案数组即可。 时间复杂度 $O(n \times n!)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。需要进行 $n!$ 次枚举,每次枚举需要 $O(n)$ 的时间来判断是否重复。另外,我们需要一个标记数组来标记每个位置是否被使用过,因此空间复杂度为 $O(n)$。 @@ -56,21 +56,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.08.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 in range(n): - if vis[j] or (j and cs[j] == cs[j - 1] and not vis[j - 1]): - continue - t[i] = cs[j] - vis[j] = True - dfs(i + 1) - vis[j] = False - - cs = sorted(S) - n = len(cs) + for j, c in enumerate(s): + if not vis[j] and (j == 0 or s[j] != s[j - 1] or vis[j - 1]): + vis[j] = True + t[i] = c + dfs(i + 1) + vis[j] = False + + s = sorted(S) ans = [] - t = [None] * n + t = s[:] + n = len(s) vis = [False] * n dfs(0) return ans @@ -80,35 +79,33 @@ class Solution: ```java class Solution { - private int n; - private char[] cs; - private List ans = new ArrayList<>(); + private char[] s; + private char[] t; private boolean[] vis; - private StringBuilder t = new StringBuilder(); + private List ans = new ArrayList<>(); public String[] permutation(String S) { - cs = S.toCharArray(); - n = cs.length; - Arrays.sort(cs); + int n = S.length(); + s = S.toCharArray(); + Arrays.sort(s); + t = new char[n]; vis = new boolean[n]; dfs(0); return ans.toArray(new String[0]); } private void dfs(int i) { - if (i == n) { - ans.add(t.toString()); + if (i >= s.length) { + ans.add(new String(t)); return; } - for (int j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + for (int j = 0; j < s.length; ++j) { + if (!vis[j] && (j == 0 || s[j] != s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.append(cs[j]); - dfs(i + 1); - t.deleteCharAt(t.length() - 1); - vis[j] = false; } } } @@ -120,26 +117,23 @@ class Solution { class Solution { public: vector permutation(string S) { - vector cs(S.begin(), S.end()); - sort(cs.begin(), cs.end()); - int n = cs.size(); - vector ans; + ranges::sort(S); + string t = S; + int n = t.size(); vector vis(n); - string t; - function dfs = [&](int i) { - if (i == n) { - ans.push_back(t); + vector ans; + auto dfs = [&](this auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); return; } for (int j = 0; j < n; ++j) { - if (vis[j] || (j && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + if (!vis[j] && (j == 0 || S[j] != S[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = S[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push_back(cs[j]); - dfs(i + 1); - t.pop_back(); - vis[j] = false; } }; dfs(0); @@ -152,26 +146,23 @@ public: ```go func permutation(S string) (ans []string) { - cs := []byte(S) - sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] }) - t := []byte{} - n := len(cs) - vis := make([]bool, n) + s := []byte(S) + sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) + t := slices.Clone(s) + vis := make([]bool, len(s)) var dfs func(int) dfs = func(i int) { - if i == n { + if i >= len(s) { ans = append(ans, string(t)) return } - for j := 0; j < n; j++ { - if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) { - continue + for j := range s { + if !vis[j] && (j == 0 || s[j] != s[j-1] || vis[j-1]) { + vis[j] = true + t[i] = s[j] + dfs(i + 1) + vis[j] = false } - vis[j] = true - t = append(t, cs[j]) - dfs(i + 1) - t = t[:len(t)-1] - vis[j] = false } } dfs(0) @@ -183,25 +174,23 @@ func permutation(S string) (ans []string) { ```ts function permutation(S: string): string[] { - const cs: string[] = S.split('').sort(); - const ans: string[] = []; - const n = cs.length; + const s: string[] = S.split('').sort(); + const n = s.length; + const t = Array(n).fill(''); const vis: boolean[] = Array(n).fill(false); - const t: string[] = []; + const ans: string[] = []; const dfs = (i: number) => { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); @@ -217,25 +206,23 @@ function permutation(S: string): string[] { * @return {string[]} */ var permutation = function (S) { - const cs = S.split('').sort(); - const ans = []; - const n = cs.length; + const s = S.split('').sort(); + const n = s.length; + const t = Array(n).fill(''); const vis = Array(n).fill(false); - const t = []; + const ans = []; const dfs = i => { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); @@ -247,36 +234,30 @@ var permutation = function (S) { ```swift class Solution { - private var n: Int = 0 - private var cs: [Character] = [] - private var ans: [String] = [] - private var vis: [Bool] = [] - private var t: String = "" - func permutation(_ S: String) -> [String] { - cs = Array(S) - n = cs.count - cs.sort() - vis = Array(repeating: false, count: n) - dfs(0) - return ans - } - - private func dfs(_ i: Int) { - if i == n { - ans.append(t) - return - } - for j in 0.. 0 && !vis[j - 1] && cs[j] == cs[j - 1]) { - continue + var ans: [String] = [] + var s: [Character] = Array(S).sorted() + var t: [Character] = Array(repeating: " ", count: s.count) + var vis: [Bool] = 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.. @@ -64,21 +64,20 @@ The time complexity is $O(n \times n!)$, and the space complexity is $O(n)$. Her 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 in range(n): - if vis[j] or (j and cs[j] == cs[j - 1] and not vis[j - 1]): - continue - t[i] = cs[j] - vis[j] = True - dfs(i + 1) - vis[j] = False - - cs = sorted(S) - n = len(cs) + for j, c in enumerate(s): + if not vis[j] and (j == 0 or s[j] != s[j - 1] or vis[j - 1]): + vis[j] = True + t[i] = c + dfs(i + 1) + vis[j] = False + + s = sorted(S) ans = [] - t = [None] * n + t = s[:] + n = len(s) vis = [False] * n dfs(0) return ans @@ -88,35 +87,33 @@ class Solution: ```java class Solution { - private int n; - private char[] cs; - private List ans = new ArrayList<>(); + private char[] s; + private char[] t; private boolean[] vis; - private StringBuilder t = new StringBuilder(); + private List ans = new ArrayList<>(); public String[] permutation(String S) { - cs = S.toCharArray(); - n = cs.length; - Arrays.sort(cs); + int n = S.length(); + s = S.toCharArray(); + Arrays.sort(s); + t = new char[n]; vis = new boolean[n]; dfs(0); return ans.toArray(new String[0]); } private void dfs(int i) { - if (i == n) { - ans.add(t.toString()); + if (i >= s.length) { + ans.add(new String(t)); return; } - for (int j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + for (int j = 0; j < s.length; ++j) { + if (!vis[j] && (j == 0 || s[j] != s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.append(cs[j]); - dfs(i + 1); - t.deleteCharAt(t.length() - 1); - vis[j] = false; } } } @@ -128,26 +125,23 @@ class Solution { class Solution { public: vector permutation(string S) { - vector cs(S.begin(), S.end()); - sort(cs.begin(), cs.end()); - int n = cs.size(); - vector ans; + ranges::sort(S); + string t = S; + int n = t.size(); vector vis(n); - string t; - function dfs = [&](int i) { - if (i == n) { - ans.push_back(t); + vector ans; + auto dfs = [&](this auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); return; } for (int j = 0; j < n; ++j) { - if (vis[j] || (j && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + if (!vis[j] && (j == 0 || S[j] != S[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = S[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push_back(cs[j]); - dfs(i + 1); - t.pop_back(); - vis[j] = false; } }; dfs(0); @@ -160,26 +154,23 @@ public: ```go func permutation(S string) (ans []string) { - cs := []byte(S) - sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] }) - t := []byte{} - n := len(cs) - vis := make([]bool, n) + s := []byte(S) + sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) + t := slices.Clone(s) + vis := make([]bool, len(s)) var dfs func(int) dfs = func(i int) { - if i == n { + if i >= len(s) { ans = append(ans, string(t)) return } - for j := 0; j < n; j++ { - if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) { - continue + for j := range s { + if !vis[j] && (j == 0 || s[j] != s[j-1] || vis[j-1]) { + vis[j] = true + t[i] = s[j] + dfs(i + 1) + vis[j] = false } - vis[j] = true - t = append(t, cs[j]) - dfs(i + 1) - t = t[:len(t)-1] - vis[j] = false } } dfs(0) @@ -191,25 +182,23 @@ func permutation(S string) (ans []string) { ```ts function permutation(S: string): string[] { - const cs: string[] = S.split('').sort(); - const ans: string[] = []; - const n = cs.length; + const s: string[] = S.split('').sort(); + const n = s.length; + const t = Array(n).fill(''); const vis: boolean[] = Array(n).fill(false); - const t: string[] = []; + const ans: string[] = []; const dfs = (i: number) => { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); @@ -225,25 +214,23 @@ function permutation(S: string): string[] { * @return {string[]} */ var permutation = function (S) { - const cs = S.split('').sort(); - const ans = []; - const n = cs.length; + const s = S.split('').sort(); + const n = s.length; + const t = Array(n).fill(''); const vis = Array(n).fill(false); - const t = []; + const ans = []; const dfs = i => { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); @@ -255,36 +242,30 @@ var permutation = function (S) { ```swift class Solution { - private var n: Int = 0 - private var cs: [Character] = [] - private var ans: [String] = [] - private var vis: [Bool] = [] - private var t: String = "" - func permutation(_ S: String) -> [String] { - cs = Array(S) - n = cs.count - cs.sort() - vis = Array(repeating: false, count: n) - dfs(0) - return ans - } - - private func dfs(_ i: Int) { - if i == n { - ans.append(t) - return - } - for j in 0.. 0 && !vis[j - 1] && cs[j] == cs[j - 1]) { - continue + var ans: [String] = [] + var s: [Character] = Array(S).sorted() + var t: [Character] = Array(repeating: " ", count: s.count) + var vis: [Bool] = 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.. permutation(string S) { - vector cs(S.begin(), S.end()); - sort(cs.begin(), cs.end()); - int n = cs.size(); - vector ans; + ranges::sort(S); + string t = S; + int n = t.size(); vector vis(n); - string t; - function dfs = [&](int i) { - if (i == n) { - ans.push_back(t); + vector ans; + auto dfs = [&](this auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); return; } for (int j = 0; j < n; ++j) { - if (vis[j] || (j && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + if (!vis[j] && (j == 0 || S[j] != S[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = S[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push_back(cs[j]); - dfs(i + 1); - t.pop_back(); - vis[j] = false; } }; dfs(0); return ans; } -}; \ No newline at end of file +}; diff --git a/lcci/08.08.Permutation II/Solution.go b/lcci/08.08.Permutation II/Solution.go index bdb449b7c9389..ce2be679b7676 100644 --- a/lcci/08.08.Permutation II/Solution.go +++ b/lcci/08.08.Permutation II/Solution.go @@ -1,26 +1,23 @@ func permutation(S string) (ans []string) { - cs := []byte(S) - sort.Slice(cs, func(i, j int) bool { return cs[i] < cs[j] }) - t := []byte{} - n := len(cs) - vis := make([]bool, n) + s := []byte(S) + sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) + t := slices.Clone(s) + vis := make([]bool, len(s)) var dfs func(int) dfs = func(i int) { - if i == n { + if i >= len(s) { ans = append(ans, string(t)) return } - for j := 0; j < n; j++ { - if vis[j] || (j > 0 && !vis[j-1] && cs[j] == cs[j-1]) { - continue + for j := range s { + if !vis[j] && (j == 0 || s[j] != s[j-1] || vis[j-1]) { + vis[j] = true + t[i] = s[j] + dfs(i + 1) + vis[j] = false } - vis[j] = true - t = append(t, cs[j]) - dfs(i + 1) - t = t[:len(t)-1] - vis[j] = false } } dfs(0) return -} \ No newline at end of file +} diff --git a/lcci/08.08.Permutation II/Solution.java b/lcci/08.08.Permutation II/Solution.java index e0dffd2d0cfae..b03452a42cd0d 100644 --- a/lcci/08.08.Permutation II/Solution.java +++ b/lcci/08.08.Permutation II/Solution.java @@ -1,33 +1,31 @@ class Solution { - private int n; - private char[] cs; - private List ans = new ArrayList<>(); + private char[] s; + private char[] t; private boolean[] vis; - private StringBuilder t = new StringBuilder(); + private List ans = new ArrayList<>(); public String[] permutation(String S) { - cs = S.toCharArray(); - n = cs.length; - Arrays.sort(cs); + int n = S.length(); + s = S.toCharArray(); + Arrays.sort(s); + t = new char[n]; vis = new boolean[n]; dfs(0); return ans.toArray(new String[0]); } private void dfs(int i) { - if (i == n) { - ans.add(t.toString()); + if (i >= s.length) { + ans.add(new String(t)); return; } - for (int j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] == cs[j - 1])) { - continue; + for (int j = 0; j < s.length; ++j) { + if (!vis[j] && (j == 0 || s[j] != s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.append(cs[j]); - dfs(i + 1); - t.deleteCharAt(t.length() - 1); - vis[j] = false; } } -} \ No newline at end of file +} diff --git a/lcci/08.08.Permutation II/Solution.js b/lcci/08.08.Permutation II/Solution.js index 44aed97be109c..4bb6b59170d9f 100644 --- a/lcci/08.08.Permutation II/Solution.js +++ b/lcci/08.08.Permutation II/Solution.js @@ -3,25 +3,23 @@ * @return {string[]} */ var permutation = function (S) { - const cs = S.split('').sort(); - const ans = []; - const n = cs.length; + const s = S.split('').sort(); + const n = s.length; + const t = Array(n).fill(''); const vis = Array(n).fill(false); - const t = []; + const ans = []; const dfs = i => { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); diff --git a/lcci/08.08.Permutation II/Solution.py b/lcci/08.08.Permutation II/Solution.py index e6b19ccba395a..e39d27633f10d 100644 --- a/lcci/08.08.Permutation II/Solution.py +++ b/lcci/08.08.Permutation II/Solution.py @@ -1,21 +1,20 @@ 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 in range(n): - if vis[j] or (j and cs[j] == cs[j - 1] and not vis[j - 1]): - continue - t[i] = cs[j] - vis[j] = True - dfs(i + 1) - vis[j] = False + for j, c in enumerate(s): + if not vis[j] and (j == 0 or s[j] != s[j - 1] or vis[j - 1]): + vis[j] = True + t[i] = c + dfs(i + 1) + vis[j] = False - cs = sorted(S) - n = len(cs) + s = sorted(S) ans = [] - t = [None] * n + t = s[:] + n = len(s) vis = [False] * n dfs(0) return ans diff --git a/lcci/08.08.Permutation II/Solution.swift b/lcci/08.08.Permutation II/Solution.swift index a00ac743ff082..84151d360dc86 100644 --- a/lcci/08.08.Permutation II/Solution.swift +++ b/lcci/08.08.Permutation II/Solution.swift @@ -1,33 +1,27 @@ class Solution { - private var n: Int = 0 - private var cs: [Character] = [] - private var ans: [String] = [] - private var vis: [Bool] = [] - private var t: String = "" - func permutation(_ S: String) -> [String] { - cs = Array(S) - n = cs.count - cs.sort() - vis = Array(repeating: false, count: n) - dfs(0) - return ans - } + var ans: [String] = [] + var s: [Character] = Array(S).sorted() + var t: [Character] = Array(repeating: " ", count: s.count) + var vis: [Bool] = Array(repeating: false, count: s.count) + let n = s.count - private func dfs(_ i: Int) { - if i == n { - ans.append(t) - return - } - for j in 0.. 0 && !vis[j - 1] && cs[j] == cs[j - 1]) { - continue + func dfs(_ i: Int) { + if i >= n { + ans.append(String(t)) + return + } + for j in 0.. { - if (i === n) { + if (i >= n) { ans.push(t.join('')); return; } for (let j = 0; j < n; ++j) { - if (vis[j] || (j > 0 && !vis[j - 1] && cs[j] === cs[j - 1])) { - continue; + if (!vis[j] && (j === 0 || s[j] !== s[j - 1] || vis[j - 1])) { + vis[j] = true; + t[i] = s[j]; + dfs(i + 1); + vis[j] = false; } - vis[j] = true; - t.push(cs[j]); - dfs(i + 1); - t.pop(); - vis[j] = false; } }; dfs(0); diff --git a/lcci/08.09.Bracket/README.md b/lcci/08.09.Bracket/README.md index 6e4a46e5769b9..7272118acce4c 100644 --- a/lcci/08.09.Bracket/README.md +++ b/lcci/08.09.Bracket/README.md @@ -104,8 +104,7 @@ class Solution { public: vector generateParenthesis(int n) { vector ans; - function dfs; - dfs = [&](int l, int r, string t) { + auto dfs = [&](this auto&& dfs, int l, int r, string t) { if (l > n || r > n || l < r) return; if (l == n && r == n) { ans.push_back(t); diff --git a/lcci/08.09.Bracket/README_EN.md b/lcci/08.09.Bracket/README_EN.md index 295bd24efe68f..36ee966e887ab 100644 --- a/lcci/08.09.Bracket/README_EN.md +++ b/lcci/08.09.Bracket/README_EN.md @@ -112,8 +112,7 @@ class Solution { public: vector generateParenthesis(int n) { vector ans; - function dfs; - dfs = [&](int l, int r, string t) { + auto dfs = [&](this auto&& dfs, int l, int r, string t) { if (l > n || r > n || l < r) return; if (l == n && r == n) { ans.push_back(t); diff --git a/lcci/08.09.Bracket/Solution.cpp b/lcci/08.09.Bracket/Solution.cpp index 4c9a371b251c4..d386301341d2c 100644 --- a/lcci/08.09.Bracket/Solution.cpp +++ b/lcci/08.09.Bracket/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: vector generateParenthesis(int n) { vector ans; - function dfs; - dfs = [&](int l, int r, string t) { + auto dfs = [&](this auto&& dfs, int l, int r, string t) { if (l > n || r > n || l < r) return; if (l == n && r == n) { ans.push_back(t); @@ -15,4 +14,4 @@ class Solution { dfs(0, 0, ""); return ans; } -}; \ No newline at end of file +};