,
) -> i32 {
if let Some(node) = root {
let node = node.borrow();
diff --git a/lcci/04.12.Paths with Sum/Solution.swift b/lcci/04.12.Paths with Sum/Solution.swift
index cf1cbe9a5b95e..ad6fb0b161171 100644
--- a/lcci/04.12.Paths with Sum/Solution.swift
+++ b/lcci/04.12.Paths with Sum/Solution.swift
@@ -1,38 +1,36 @@
-/* class TreeNode {
-* var val: Int
-* var left: TreeNode?
-* var right: TreeNode?
-*
-* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
-* self.val = val
-* self.left = left
-* self.right = right
-* }
-* }
-*/
-
+/**
+ * Definition for a binary tree node.
+ * public class TreeNode {
+ * public var val: Int
+ * public var left: TreeNode?
+ * public var right: TreeNode?
+ * public init() { self.val = 0; self.left = nil; self.right = nil; }
+ * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
+ * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
+ * self.val = val
+ * self.left = left
+ * self.right = right
+ * }
+ * }
+ */
class Solution {
- private var cnt: [Int: Int] = [:]
- private var target: Int = 0
-
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
- cnt[0] = 1
- target = sum
- return dfs(root, 0)
- }
+ var cnt: [Int: Int] = [0: 1]
+
+ func dfs(_ root: TreeNode?, _ s: Int) -> Int {
+ guard let root = root else { return 0 }
- private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
- guard let root = root else {
- return 0
+ var s = s + root.val
+ var ans = cnt[s - sum, default: 0]
+
+ cnt[s, default: 0] += 1
+ ans += dfs(root.left, s)
+ ans += dfs(root.right, s)
+ cnt[s, default: 0] -= 1
+
+ return ans
}
- let newSum = s + root.val
- let ans = cnt[newSum - target, default: 0]
-
- cnt[newSum, default: 0] += 1
- let leftPaths = dfs(root.left, newSum)
- let rightPaths = dfs(root.right, newSum)
- cnt[newSum, default: 0] -= 1
-
- return ans + leftPaths + rightPaths
+
+ return dfs(root, 0)
}
-}
\ No newline at end of file
+}
diff --git a/lcci/05.02.Binary Number to String/README.md b/lcci/05.02.Binary Number to String/README.md
index e65e49dbeebdc..2064ff9d442ee 100644
--- a/lcci/05.02.Binary Number to String/README.md
+++ b/lcci/05.02.Binary Number to String/README.md
@@ -42,10 +42,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.02.Binary%20Number
$$
\begin{aligned}
-0.8125 \times 2 &= 1.625 \quad \text{取整数部分} \quad 1 \\
-0.625 \times 2 &= 1.25 \quad \text{取整数部分} \quad 1 \\
-0.25 \times 2 &= 0.5 \quad \text{取整数部分} \quad 0 \\
-0.5 \times 2 &= 1 \quad \text{取整数部分} \quad 1 \\
+0.8125 \times 2 &= 1.625 \quad \textit{取整数部分} \quad 1 \\
+0.625 \times 2 &= 1.25 \quad \textit{取整数部分} \quad 1 \\
+0.25 \times 2 &= 0.5 \quad \textit{取整数部分} \quad 0 \\
+0.5 \times 2 &= 1 \quad \textit{取整数部分} \quad 1 \\
\end{aligned}
$$
diff --git a/lcci/05.02.Binary Number to String/README_EN.md b/lcci/05.02.Binary Number to String/README_EN.md
index c54d0c6cad2b3..39edca43b3d3b 100644
--- a/lcci/05.02.Binary Number to String/README_EN.md
+++ b/lcci/05.02.Binary Number to String/README_EN.md
@@ -51,10 +51,10 @@ Let's take an example, suppose we want to convert $0.8125$ to a binary fraction,
$$
\begin{aligned}
-0.8125 \times 2 &= 1.625 \quad \text{take the integer part} \quad 1 \\
-0.625 \times 2 &= 1.25 \quad \text{take the integer part} \quad 1 \\
-0.25 \times 2 &= 0.5 \quad \text{take the integer part} \quad 0 \\
-0.5 \times 2 &= 1 \quad \text{take the integer part} \quad 1 \\
+0.8125 \times 2 &= 1.625 \quad \textit{take the integer part} \quad 1 \\
+0.625 \times 2 &= 1.25 \quad \textit{take the integer part} \quad 1 \\
+0.25 \times 2 &= 0.5 \quad \textit{take the integer part} \quad 0 \\
+0.5 \times 2 &= 1 \quad \textit{take the integer part} \quad 1 \\
\end{aligned}
$$
diff --git a/lcci/05.03.Reverse Bits/README.md b/lcci/05.03.Reverse Bits/README.md
index cbec7bc2a611d..fad13598cbc1b 100644
--- a/lcci/05.03.Reverse Bits/README.md
+++ b/lcci/05.03.Reverse Bits/README.md
@@ -134,13 +134,13 @@ function reverseBits(num: number): number {
class Solution {
func reverseBits(_ num: Int) -> Int {
var ans = 0
- var countZeros = 0
+ var cnt = 0
var j = 0
for i in 0..<32 {
- countZeros += (num >> i & 1 ^ 1)
- while countZeros > 1 {
- countZeros -= (num >> j & 1 ^ 1)
+ cnt += (num >> i & 1 ^ 1)
+ while cnt > 1 {
+ cnt -= (num >> j & 1 ^ 1)
j += 1
}
ans = max(ans, i - j + 1)
diff --git a/lcci/05.03.Reverse Bits/README_EN.md b/lcci/05.03.Reverse Bits/README_EN.md
index eb68a91114262..a9c88a668e959 100644
--- a/lcci/05.03.Reverse Bits/README_EN.md
+++ b/lcci/05.03.Reverse Bits/README_EN.md
@@ -142,13 +142,13 @@ function reverseBits(num: number): number {
class Solution {
func reverseBits(_ num: Int) -> Int {
var ans = 0
- var countZeros = 0
+ var cnt = 0
var j = 0
for i in 0..<32 {
- countZeros += (num >> i & 1 ^ 1)
- while countZeros > 1 {
- countZeros -= (num >> j & 1 ^ 1)
+ cnt += (num >> i & 1 ^ 1)
+ while cnt > 1 {
+ cnt -= (num >> j & 1 ^ 1)
j += 1
}
ans = max(ans, i - j + 1)
diff --git a/lcci/05.03.Reverse Bits/Solution.swift b/lcci/05.03.Reverse Bits/Solution.swift
index da1c7fe2acf4a..0bd69d4893f51 100644
--- a/lcci/05.03.Reverse Bits/Solution.swift
+++ b/lcci/05.03.Reverse Bits/Solution.swift
@@ -1,13 +1,13 @@
class Solution {
func reverseBits(_ num: Int) -> Int {
var ans = 0
- var countZeros = 0
+ var cnt = 0
var j = 0
for i in 0..<32 {
- countZeros += (num >> i & 1 ^ 1)
- while countZeros > 1 {
- countZeros -= (num >> j & 1 ^ 1)
+ cnt += (num >> i & 1 ^ 1)
+ while cnt > 1 {
+ cnt -= (num >> j & 1 ^ 1)
j += 1
}
ans = max(ans, i - j + 1)
diff --git a/lcci/05.07.Exchange/README.md b/lcci/05.07.Exchange/README.md
index ea45ce6241d41..a6c96240f2569 100644
--- a/lcci/05.07.Exchange/README.md
+++ b/lcci/05.07.Exchange/README.md
@@ -44,7 +44,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.07.Exchange/README
### 方法一:位运算
-我们可以将 $\text{num}$ 与 $\text{0x55555555}$ 进行与运算,得到的结果是 $\text{num}$ 的偶数位,然后将其左移一位。再将 $\text{num}$ 与 $\text{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\text{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。
+我们可以将 $\textit{num}$ 与 $\textit{0x55555555}$ 进行与运算,得到的结果是 $\textit{num}$ 的偶数位,然后将其左移一位。再将 $\textit{num}$ 与 $\textit{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\textit{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md
index 421461ed9d774..93eacbfea15f8 100644
--- a/lcci/08.01.Three Steps Problem/README.md
+++ b/lcci/08.01.Three Steps Problem/README.md
@@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2
示例1:
- 输入:n = 3
+ 输入:n = 3
输出:4
说明: 有四种走法
@@ -226,37 +226,6 @@ $$
#### Python3
-```python
-class Solution:
- def waysToStep(self, n: int) -> int:
- mod = 10**9 + 7
-
- def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
- m, n = len(a), len(b[0])
- c = [[0] * n for _ in range(m)]
- for i in range(m):
- for j in range(n):
- for k in range(len(a[0])):
- c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
- return c
-
- def pow(a: List[List[int]], n: int) -> List[List[int]]:
- res = [[4, 2, 1]]
- while n:
- if n & 1:
- res = mul(res, a)
- n >>= 1
- a = mul(a, a)
- return res
-
- if n < 4:
- return 2 ** (n - 1)
- a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
- return sum(pow(a, n - 4)[0]) % mod
-```
-
-#### Python3
-
```python
import numpy as np
@@ -266,8 +235,8 @@ class Solution:
if n < 4:
return 2 ** (n - 1)
mod = 10**9 + 7
- factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
- res = np.mat([(4, 2, 1)], np.dtype("O"))
+ factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
+ res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
n -= 4
while n:
if n & 1:
diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md
index 93d93478b8bef..1f10f60a219b3 100644
--- a/lcci/08.01.Three Steps Problem/README_EN.md
+++ b/lcci/08.01.Three Steps Problem/README_EN.md
@@ -20,7 +20,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2
- Input: n = 3
+ Input: n = 3
Output: 4
@@ -229,37 +229,6 @@ The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
#### Python3
-```python
-class Solution:
- def waysToStep(self, n: int) -> int:
- mod = 10**9 + 7
-
- def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
- m, n = len(a), len(b[0])
- c = [[0] * n for _ in range(m)]
- for i in range(m):
- for j in range(n):
- for k in range(len(a[0])):
- c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
- return c
-
- def pow(a: List[List[int]], n: int) -> List[List[int]]:
- res = [[4, 2, 1]]
- while n:
- if n & 1:
- res = mul(res, a)
- n >>= 1
- a = mul(a, a)
- return res
-
- if n < 4:
- return 2 ** (n - 1)
- a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
- return sum(pow(a, n - 4)[0]) % mod
-```
-
-#### Python3
-
```python
import numpy as np
@@ -269,8 +238,8 @@ class Solution:
if n < 4:
return 2 ** (n - 1)
mod = 10**9 + 7
- factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
- res = np.mat([(4, 2, 1)], np.dtype("O"))
+ factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
+ res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
n -= 4
while n:
if n & 1:
diff --git a/lcci/08.01.Three Steps Problem/Solution2.py b/lcci/08.01.Three Steps Problem/Solution2.py
index 47973c3c6b40d..ce8bd1b06daa3 100644
--- a/lcci/08.01.Three Steps Problem/Solution2.py
+++ b/lcci/08.01.Three Steps Problem/Solution2.py
@@ -1,26 +1,17 @@
-class Solution:
- def waysToStep(self, n: int) -> int:
- mod = 10**9 + 7
-
- def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
- m, n = len(a), len(b[0])
- c = [[0] * n for _ in range(m)]
- for i in range(m):
- for j in range(n):
- for k in range(len(a[0])):
- c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
- return c
+import numpy as np
- def pow(a: List[List[int]], n: int) -> List[List[int]]:
- res = [[4, 2, 1]]
- while n:
- if n & 1:
- res = mul(res, a)
- n >>= 1
- a = mul(a, a)
- return res
+class Solution:
+ def waysToStep(self, n: int) -> int:
if n < 4:
return 2 ** (n - 1)
- a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
- return sum(pow(a, n - 4)[0]) % mod
+ mod = 10**9 + 7
+ factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
+ res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
+ n -= 4
+ while n:
+ if n & 1:
+ res = res * factor % mod
+ factor = factor * factor % mod
+ n >>= 1
+ return res.sum() % mod
diff --git a/lcci/08.01.Three Steps Problem/Solution3.py b/lcci/08.01.Three Steps Problem/Solution3.py
deleted file mode 100644
index 7ee79117fbbaa..0000000000000
--- a/lcci/08.01.Three Steps Problem/Solution3.py
+++ /dev/null
@@ -1,17 +0,0 @@
-import numpy as np
-
-
-class Solution:
- def waysToStep(self, n: int) -> int:
- if n < 4:
- return 2 ** (n - 1)
- mod = 10**9 + 7
- factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
- res = np.mat([(4, 2, 1)], np.dtype("O"))
- n -= 4
- while n:
- if n & 1:
- res = res * factor % mod
- factor = factor * factor % mod
- n >>= 1
- return res.sum() % mod
diff --git a/lcci/08.02.Robot in a Grid/README.md b/lcci/08.02.Robot in a Grid/README.md
index a13e2f594e13c..8b60835e73b0c 100644
--- a/lcci/08.02.Robot in a Grid/README.md
+++ b/lcci/08.02.Robot in a Grid/README.md
@@ -15,7 +15,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%
设想有个机器人坐在一个网格的左上角,网格 r 行 c 列。机器人只能向下或向右移动,但不能走到一些被禁止的网格(有障碍物)。设计一种算法,寻找机器人从左上角移动到右下角的路径。
+

+
网格中的障碍物和空位置分别用 1
和 0
来表示。
返回一条可行的路径,路径由经过的网格的行号和列号组成。左上角为 0 行 0 列。
示例 1:
@@ -26,7 +28,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%
[0,0,0]
]
输出: [[0,0],[0,1],[0,2],[1,2],[2,2]]
-解释:
+解释:
输入中标粗的位置即为输出表示的路径,即
0行0列(左上角) -> 0行1列 -> 0行2列 -> 1行2列 -> 2行2列(右下角)
说明:r 和 c 的值均不超过 100。
@@ -107,7 +109,7 @@ public:
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector> ans;
- function dfs = [&](int i, int j) -> bool {
+ auto dfs = [&](this auto&& dfs, int i, int j) -> bool {
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
return false;
}
@@ -186,10 +188,9 @@ impl Solution {
}
path.push(vec![i as i32, j as i32]);
grid[i as usize][j as usize] = 1;
- if
- (i + 1 == grid.len() && j + 1 == grid[0].len()) ||
- Self::dfs(grid, path, i + 1, j) ||
- Self::dfs(grid, path, i, j + 1)
+ if (i + 1 == grid.len() && j + 1 == grid[0].len())
+ || Self::dfs(grid, path, i + 1, j)
+ || Self::dfs(grid, path, i, j + 1)
{
return true;
}
diff --git a/lcci/08.02.Robot in a Grid/README_EN.md b/lcci/08.02.Robot in a Grid/README_EN.md
index 07603c08178fc..bb37b1b1bf1d9 100644
--- a/lcci/08.02.Robot in a Grid/README_EN.md
+++ b/lcci/08.02.Robot in a Grid/README_EN.md
@@ -15,7 +15,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%
Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can only move in two directions, right and down, but certain cells are "off limits" such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.
+

+
"off limits" and empty grid are represented by 1
and 0
respectively.
Return a valid path, consisting of row number and column number of grids in the path.
Example 1:
@@ -116,7 +118,7 @@ public:
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector> ans;
- function dfs = [&](int i, int j) -> bool {
+ auto dfs = [&](this auto&& dfs, int i, int j) -> bool {
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
return false;
}
@@ -195,10 +197,9 @@ impl Solution {
}
path.push(vec![i as i32, j as i32]);
grid[i as usize][j as usize] = 1;
- if
- (i + 1 == grid.len() && j + 1 == grid[0].len()) ||
- Self::dfs(grid, path, i + 1, j) ||
- Self::dfs(grid, path, i, j + 1)
+ if (i + 1 == grid.len() && j + 1 == grid[0].len())
+ || Self::dfs(grid, path, i + 1, j)
+ || Self::dfs(grid, path, i, j + 1)
{
return true;
}
diff --git a/lcci/08.02.Robot in a Grid/Solution.cpp b/lcci/08.02.Robot in a Grid/Solution.cpp
index e9657e05ef757..f7daef341d833 100644
--- a/lcci/08.02.Robot in a Grid/Solution.cpp
+++ b/lcci/08.02.Robot in a Grid/Solution.cpp
@@ -4,7 +4,7 @@ class Solution {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector> ans;
- function dfs = [&](int i, int j) -> bool {
+ auto dfs = [&](this auto&& dfs, int i, int j) -> bool {
if (i >= m || j >= n || obstacleGrid[i][j] == 1) {
return false;
}
@@ -18,4 +18,4 @@ class Solution {
};
return dfs(0, 0) ? ans : vector>();
}
-};
\ No newline at end of file
+};
diff --git a/lcci/08.02.Robot in a Grid/Solution.rs b/lcci/08.02.Robot in a Grid/Solution.rs
index 6c6e7a5d7adad..3f121643bd9ce 100644
--- a/lcci/08.02.Robot in a Grid/Solution.rs
+++ b/lcci/08.02.Robot in a Grid/Solution.rs
@@ -5,10 +5,9 @@ impl Solution {
}
path.push(vec![i as i32, j as i32]);
grid[i as usize][j as usize] = 1;
- if
- (i + 1 == grid.len() && j + 1 == grid[0].len()) ||
- Self::dfs(grid, path, i + 1, j) ||
- Self::dfs(grid, path, i, j + 1)
+ if (i + 1 == grid.len() && j + 1 == grid[0].len())
+ || Self::dfs(grid, path, i + 1, j)
+ || Self::dfs(grid, path, i, j + 1)
{
return true;
}
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
+};
diff --git a/lcci/10.02.Group Anagrams/Solution.rs b/lcci/10.02.Group Anagrams/Solution.rs
index 85ae009c61d49..4773defd8a6c6 100644
--- a/lcci/10.02.Group Anagrams/Solution.rs
+++ b/lcci/10.02.Group Anagrams/Solution.rs
@@ -9,9 +9,7 @@ impl Solution {
cs.sort();
cs.iter().collect::()
};
- map.entry(key)
- .or_insert(vec![])
- .push(s);
+ map.entry(key).or_insert(vec![]).push(s);
}
map.into_values().collect()
}
diff --git a/lcci/10.09.Sorted Matrix Search/README.md b/lcci/10.09.Sorted Matrix Search/README.md
index f1678a642d65f..0cf5d1ed6b3b8 100644
--- a/lcci/10.09.Sorted Matrix Search/README.md
+++ b/lcci/10.09.Sorted Matrix Search/README.md
@@ -249,9 +249,9 @@ class Solution {
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]`与 `target` 的大小关系:
-- 若 $\text{matrix}[i][j] = \text{target}$,说明找到了目标值,直接返回 `true`。
-- 若 $\text{matrix}[i][j] > \text{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
-- 若 $\text{matrix}[i][j] < \text{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
+- 若 $\textit{matrix}[i][j] = \textit{target}$,说明找到了目标值,直接返回 `true`。
+- 若 $\textit{matrix}[i][j] > \textit{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
+- 若 $\textit{matrix}[i][j] < \textit{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
若搜索结束依然找不到 `target`,返回 `false`。
diff --git a/lcci/10.09.Sorted Matrix Search/README_EN.md b/lcci/10.09.Sorted Matrix Search/README_EN.md
index 737900835597c..9cf63c625e0c6 100644
--- a/lcci/10.09.Sorted Matrix Search/README_EN.md
+++ b/lcci/10.09.Sorted Matrix Search/README_EN.md
@@ -258,9 +258,9 @@ class Solution {
Here, we start searching from the bottom left corner and move towards the top right direction, comparing the current element `matrix[i][j]` with `target`:
-- If $\text{matrix}[i][j] = \text{target}$, it means the target value has been found, and we directly return `true`.
-- If $\text{matrix}[i][j] > \text{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
-- If $\text{matrix}[i][j] < \text{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
+- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value has been found, and we directly return `true`.
+- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
+- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
If the search ends and the `target` is still not found, return `false`.
diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md
index 96046d7d1dd37..af8891fe3f4e9 100644
--- a/lcci/16.02.Words Frequency/README.md
+++ b/lcci/16.02.Words Frequency/README.md
@@ -194,11 +194,7 @@ impl WordsFrequency {
fn get(&self, word: String) -> i32 {
*self.cnt.get(&word).unwrap_or(&0)
}
-}/**
- * Your WordsFrequency object will be instantiated and called as such:
- * let obj = WordsFrequency::new(book);
- * let ret_1: i32 = obj.get(word);
- */
+}
```
#### JavaScript
diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md
index 91b1267402184..8d3a09401c7a2 100644
--- a/lcci/16.02.Words Frequency/README_EN.md
+++ b/lcci/16.02.Words Frequency/README_EN.md
@@ -208,11 +208,7 @@ impl WordsFrequency {
fn get(&self, word: String) -> i32 {
*self.cnt.get(&word).unwrap_or(&0)
}
-}/**
- * Your WordsFrequency object will be instantiated and called as such:
- * let obj = WordsFrequency::new(book);
- * let ret_1: i32 = obj.get(word);
- */
+}
```
#### JavaScript
diff --git a/lcci/16.02.Words Frequency/Solution.rs b/lcci/16.02.Words Frequency/Solution.rs
index bbab4feff97a9..f136c6d6242f2 100644
--- a/lcci/16.02.Words Frequency/Solution.rs
+++ b/lcci/16.02.Words Frequency/Solution.rs
@@ -19,8 +19,4 @@ impl WordsFrequency {
fn get(&self, word: String) -> i32 {
*self.cnt.get(&word).unwrap_or(&0)
}
-}/**
- * Your WordsFrequency object will be instantiated and called as such:
- * let obj = WordsFrequency::new(book);
- * let ret_1: i32 = obj.get(word);
- */
+}
diff --git a/lcci/16.25.LRU Cache/README.md b/lcci/16.25.LRU Cache/README.md
index 68f73a60b87bf..2faece705d8ad 100644
--- a/lcci/16.25.LRU Cache/README.md
+++ b/lcci/16.25.LRU Cache/README.md
@@ -548,12 +548,7 @@ impl LRUCache {
None => None,
}
}
-}/**
- * Your LRUCache object will be instantiated and called as such:
- * let obj = LRUCache::new(capacity);
- * let ret_1: i32 = obj.get(key);
- * obj.put(key, value);
- */
+}
```
#### C#
diff --git a/lcci/16.25.LRU Cache/README_EN.md b/lcci/16.25.LRU Cache/README_EN.md
index 11e480ea76183..afedd5b11b2af 100644
--- a/lcci/16.25.LRU Cache/README_EN.md
+++ b/lcci/16.25.LRU Cache/README_EN.md
@@ -67,7 +67,7 @@ When accessing a node, if the node exists, we delete it from its original positi
When inserting a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. If it does not exist, we first check if the cache is full. If it is full, we delete the node at the tail of the list and insert the new node at the head of the list.
-The time complexity is $O(1)$, and the space complexity is $O(\text{capacity})$.
+The time complexity is $O(1)$, and the space complexity is $O(\textit{capacity})$.
@@ -562,12 +562,7 @@ impl LRUCache {
None => None,
}
}
-}/**
- * Your LRUCache object will be instantiated and called as such:
- * let obj = LRUCache::new(capacity);
- * let ret_1: i32 = obj.get(key);
- * obj.put(key, value);
- */
+}
```
#### C#
diff --git a/lcci/16.25.LRU Cache/Solution.rs b/lcci/16.25.LRU Cache/Solution.rs
index 76b8fecb7769d..c02f601eb4a60 100644
--- a/lcci/16.25.LRU Cache/Solution.rs
+++ b/lcci/16.25.LRU Cache/Solution.rs
@@ -120,9 +120,4 @@ impl LRUCache {
None => None,
}
}
-}/**
- * Your LRUCache object will be instantiated and called as such:
- * let obj = LRUCache::new(capacity);
- * let ret_1: i32 = obj.get(key);
- * obj.put(key, value);
- */
+}
diff --git a/lcci/17.15.Longest Word/README.md b/lcci/17.15.Longest Word/README.md
index a9840705c9c6a..0f6dffe5c3aa8 100644
--- a/lcci/17.15.Longest Word/README.md
+++ b/lcci/17.15.Longest Word/README.md
@@ -32,117 +32,79 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/17.15.Longest%20Word/
-### 方法一:前缀树 + DFS
+### 方法一:哈希表 + 排序 + DFS
+
+注意,题目中,每个单词实际上允许重复使用。
+
+我们可以用一个哈希表 $\textit{s}$ 存储所有单词,然后对单词按照长度降序排序,如果长度相同,按照字典序升序排序。
+
+接下来,我们遍历排序后的单词列表,对于每个单词 $\textit{w}$,我们先将其从哈希表 $\textit{s}$ 中移除,然后使用深度优先搜索 $\textit{dfs}$ 判断 $\textit{w}$ 是否可以由其他单词组成,如果可以,返回 $\textit{w}$。
+
+函数 $\textit{dfs}$ 的执行逻辑如下:
+
+- 如果 $\textit{w}$ 为空,返回 $\text{true}$;
+- 遍历 $\textit{w}$ 的所有前缀,如果前缀在哈希表 $\textit{s}$ 中且 $\textit{dfs}$ 返回 $\text{true}$,则返回 $\text{true}$;
+- 如果没有符合条件的前缀,返回 $\text{false}$。
+
+如果没有找到符合条件的单词,返回空字符串。
+
+时间复杂度 $O(m \times n \times \log n + n \times 2^M)$,空间复杂度 $O(m \times n)$。其中 $n$ 和 $m$ 分别为单词列表的长度和单词的平均长度,而 $M$ 为最长单词的长度。
#### Python3
```python
-class Trie:
- def __init__(self):
- self.children = [None] * 26
- self.is_end = False
-
- def insert(self, word):
- node = self
- for c in word:
- idx = ord(c) - ord('a')
- if node.children[idx] is None:
- node.children[idx] = Trie()
- node = node.children[idx]
- node.is_end = True
-
- def search(self, word):
- node = self
- for c in word:
- idx = ord(c) - ord('a')
- if node.children[idx] is None:
- return False
- node = node.children[idx]
- return node.is_end
-
-
class Solution:
def longestWord(self, words: List[str]) -> str:
- def cmp(a, b):
- if len(a) != len(b):
- return len(a) - len(b)
- return -1 if a > b else 1
-
- def dfs(w):
- return not w or any(
- trie.search(w[:i]) and dfs(w[i:]) for i in range(1, len(w) + 1)
- )
-
- words.sort(key=cmp_to_key(cmp))
- trie = Trie()
- ans = ""
+ def dfs(w: str) -> bool:
+ if not w:
+ return True
+ for k in range(1, len(w) + 1):
+ if w[:k] in s and dfs(w[k:]):
+ return True
+ return False
+
+ s = set(words)
+ words.sort(key=lambda x: (-len(x), x))
for w in words:
+ s.remove(w)
if dfs(w):
- ans = w
- trie.insert(w)
- return ans
+ return w
+ return ""
```
#### Java
```java
-class Trie {
- Trie[] children = new Trie[26];
- boolean isEnd;
-
- void insert(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- node.children[c] = new Trie();
- }
- node = node.children[c];
- }
- node.isEnd = true;
- }
-
- boolean search(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- return false;
- }
- node = node.children[c];
- }
- return node.isEnd;
- }
-}
-
class Solution {
- private Trie trie = new Trie();
+ private Set s = new HashSet<>();
public String longestWord(String[] words) {
+ for (String w : words) {
+ s.add(w);
+ }
Arrays.sort(words, (a, b) -> {
if (a.length() != b.length()) {
- return a.length() - b.length();
+ return b.length() - a.length();
}
- return b.compareTo(a);
+ return a.compareTo(b);
});
- String ans = "";
for (String w : words) {
+ s.remove(w);
if (dfs(w)) {
- ans = w;
+ return w;
}
- trie.insert(w);
}
- return ans;
+ return "";
}
private boolean dfs(String w) {
- if ("".equals(w)) {
+ if (w.length() == 0) {
return true;
}
- for (int i = 1; i <= w.length(); ++i) {
- if (trie.search(w.substring(0, i)) && dfs(w.substring(i))) {
+ for (int k = 1; k <= w.length(); ++k) {
+ if (s.contains(w.substring(0, k)) && dfs(w.substring(k))) {
return true;
}
}
@@ -151,131 +113,171 @@ class Solution {
}
```
-#### Go
-
-```go
-type Trie struct {
- children [26]*Trie
- isEnd bool
-}
+#### C++
-func newTrie() *Trie {
- return &Trie{}
-}
-func (this *Trie) insert(word string) {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- node.children[c] = newTrie()
- }
- node = node.children[c]
- }
- node.isEnd = true
-}
+```cpp
+class Solution {
+public:
+ string longestWord(vector& words) {
+ unordered_set s(words.begin(), words.end());
+ ranges::sort(words, [&](const string& a, const string& b) {
+ return a.size() > b.size() || (a.size() == b.size() && a < b);
+ });
+ auto dfs = [&](this auto&& dfs, string w) -> bool {
+ if (w.empty()) {
+ return true;
+ }
+ for (int k = 1; k <= w.size(); ++k) {
+ if (s.contains(w.substr(0, k)) && dfs(w.substr(k))) {
+ return true;
+ }
+ }
+ return false;
+ };
+ for (const string& w : words) {
+ s.erase(w);
+ if (dfs(w)) {
+ return w;
+ }
+ }
+ return "";
+ }
+};
+```
-func (this *Trie) search(word string) bool {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- return false
- }
- node = node.children[c]
- }
- return node.isEnd
-}
+#### Go
+```go
func longestWord(words []string) string {
+ s := map[string]bool{}
+ for _, w := range words {
+ s[w] = true
+ }
sort.Slice(words, func(i, j int) bool {
- a, b := words[i], words[j]
- if len(a) != len(b) {
- return len(a) < len(b)
- }
- return a > b
+ return len(words[i]) > len(words[j]) || (len(words[i]) == len(words[j]) && words[i] < words[j])
})
- trie := newTrie()
var dfs func(string) bool
dfs = func(w string) bool {
if len(w) == 0 {
return true
}
- for i := 1; i <= len(w); i++ {
- if trie.search(w[:i]) && dfs(w[i:]) {
+ for k := 1; k <= len(w); k++ {
+ if s[w[:k]] && dfs(w[k:]) {
return true
}
}
return false
}
- ans := ""
for _, w := range words {
+ s[w] = false
if dfs(w) {
- ans = w
+ return w
}
- trie.insert(w)
}
- return ans
+ return ""
}
```
-#### Swift
+#### TypeScript
-```swift
-class Trie {
- var children = [Trie?](repeating: nil, count: 26)
- var isEnd = false
-
- func insert(_ word: String) {
- var node = self
- for ch in word {
- let index = Int(ch.asciiValue! - Character("a").asciiValue!)
- if node.children[index] == nil {
- node.children[index] = Trie()
+```ts
+function longestWord(words: string[]): string {
+ const s = new Set(words);
+
+ words.sort((a, b) => (a.length === b.length ? a.localeCompare(b) : b.length - a.length));
+
+ const dfs = (w: string): boolean => {
+ if (w === '') {
+ return true;
+ }
+ for (let k = 1; k <= w.length; ++k) {
+ if (s.has(w.substring(0, k)) && dfs(w.substring(k))) {
+ return true;
}
- node = node.children[index]!
}
- node.isEnd = true
+ return false;
+ };
+
+ for (const w of words) {
+ s.delete(w);
+ if (dfs(w)) {
+ return w;
+ }
}
- func search(_ word: String) -> Bool {
- var node = self
- for ch in word {
- let index = Int(ch.asciiValue! - Character("a").asciiValue!)
- if node.children[index] == nil {
- return false
+ return '';
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn longest_word(words: Vec) -> String {
+ let mut s: HashSet = words.iter().cloned().collect();
+ let mut words = words;
+ words.sort_by(|a, b| b.len().cmp(&a.len()).then(a.cmp(b)));
+
+ fn dfs(w: String, s: &mut HashSet) -> bool {
+ if w.is_empty() {
+ return true;
}
- node = node.children[index]!
+ for k in 1..=w.len() {
+ if s.contains(&w[0..k]) && dfs(w[k..].to_string(), s) {
+ return true;
+ }
+ }
+ false
}
- return node.isEnd
+ for w in words {
+ s.remove(&w);
+ if dfs(w.clone(), &mut s) {
+ return w;
+ }
+ }
+ String::new()
}
}
+```
+
+#### Swift
+```swift
class Solution {
func longestWord(_ words: [String]) -> String {
- var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
- let trie = Trie()
+ var s: Set = Set(words)
+ var words = words
+ words.sort { (a, b) -> Bool in
+ if a.count == b.count {
+ return a < b
+ } else {
+ return a.count > b.count
+ }
+ }
- var dfs: ((String) -> Bool)!
- dfs = { w in
+ func dfs(_ w: String) -> Bool {
if w.isEmpty {
return true
}
- for i in 1...w.count {
- if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
+ for k in 1...w.count {
+ let prefix = String(w.prefix(k))
+ if s.contains(prefix) && dfs(String(w.dropFirst(k))) {
return true
}
}
return false
}
- var ans = ""
for w in words {
+ s.remove(w)
if dfs(w) {
- ans = w
+ return w
}
- trie.insert(w)
}
- return ans
+
+ return ""
}
}
```
diff --git a/lcci/17.15.Longest Word/README_EN.md b/lcci/17.15.Longest Word/README_EN.md
index 7d5dcf7f14dec..131326adf233f 100644
--- a/lcci/17.15.Longest Word/README_EN.md
+++ b/lcci/17.15.Longest Word/README_EN.md
@@ -41,117 +41,79 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/17.15.Longest%20Word/
-### Solution 1
+### Solution 1: Hash Table + Sorting + DFS
+
+Note that in the problem, each word can actually be reused.
+
+We can use a hash table $\textit{s}$ to store all the words, then sort the words in descending order of length, and if the lengths are the same, sort them in ascending lexicographical order.
+
+Next, we iterate through the sorted list of words. For each word $\textit{w}$, we first remove it from the hash table $\textit{s}$, then use depth-first search $\textit{dfs}$ to determine if $\textit{w}$ can be composed of other words. If it can, we return $\textit{w}$.
+
+The execution logic of the function $\textit{dfs}$ is as follows:
+
+- If $\textit{w}$ is empty, return $\text{true}$;
+- Iterate through all prefixes of $\textit{w}$. If a prefix is in the hash table $\textit{s}$ and $\textit{dfs}$ returns $\text{true}$, then return $\text{true}$;
+- If no prefix meets the condition, return $\text{false}$.
+
+If no word meets the condition, return an empty string.
+
+The time complexity is $O(m \times n \times \log n + n \times 2^M)$, and the space complexity is $O(m \times n)$. Here, $n$ and $m$ are the length of the word list and the average length of the words, respectively, and $M$ is the length of the longest word.
#### Python3
```python
-class Trie:
- def __init__(self):
- self.children = [None] * 26
- self.is_end = False
-
- def insert(self, word):
- node = self
- for c in word:
- idx = ord(c) - ord('a')
- if node.children[idx] is None:
- node.children[idx] = Trie()
- node = node.children[idx]
- node.is_end = True
-
- def search(self, word):
- node = self
- for c in word:
- idx = ord(c) - ord('a')
- if node.children[idx] is None:
- return False
- node = node.children[idx]
- return node.is_end
-
-
class Solution:
def longestWord(self, words: List[str]) -> str:
- def cmp(a, b):
- if len(a) != len(b):
- return len(a) - len(b)
- return -1 if a > b else 1
-
- def dfs(w):
- return not w or any(
- trie.search(w[:i]) and dfs(w[i:]) for i in range(1, len(w) + 1)
- )
-
- words.sort(key=cmp_to_key(cmp))
- trie = Trie()
- ans = ""
+ def dfs(w: str) -> bool:
+ if not w:
+ return True
+ for k in range(1, len(w) + 1):
+ if w[:k] in s and dfs(w[k:]):
+ return True
+ return False
+
+ s = set(words)
+ words.sort(key=lambda x: (-len(x), x))
for w in words:
+ s.remove(w)
if dfs(w):
- ans = w
- trie.insert(w)
- return ans
+ return w
+ return ""
```
#### Java
```java
-class Trie {
- Trie[] children = new Trie[26];
- boolean isEnd;
-
- void insert(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- node.children[c] = new Trie();
- }
- node = node.children[c];
- }
- node.isEnd = true;
- }
-
- boolean search(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- return false;
- }
- node = node.children[c];
- }
- return node.isEnd;
- }
-}
-
class Solution {
- private Trie trie = new Trie();
+ private Set s = new HashSet<>();
public String longestWord(String[] words) {
+ for (String w : words) {
+ s.add(w);
+ }
Arrays.sort(words, (a, b) -> {
if (a.length() != b.length()) {
- return a.length() - b.length();
+ return b.length() - a.length();
}
- return b.compareTo(a);
+ return a.compareTo(b);
});
- String ans = "";
for (String w : words) {
+ s.remove(w);
if (dfs(w)) {
- ans = w;
+ return w;
}
- trie.insert(w);
}
- return ans;
+ return "";
}
private boolean dfs(String w) {
- if ("".equals(w)) {
+ if (w.length() == 0) {
return true;
}
- for (int i = 1; i <= w.length(); ++i) {
- if (trie.search(w.substring(0, i)) && dfs(w.substring(i))) {
+ for (int k = 1; k <= w.length(); ++k) {
+ if (s.contains(w.substring(0, k)) && dfs(w.substring(k))) {
return true;
}
}
@@ -160,131 +122,171 @@ class Solution {
}
```
-#### Go
-
-```go
-type Trie struct {
- children [26]*Trie
- isEnd bool
-}
+#### C++
-func newTrie() *Trie {
- return &Trie{}
-}
-func (this *Trie) insert(word string) {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- node.children[c] = newTrie()
- }
- node = node.children[c]
- }
- node.isEnd = true
-}
+```cpp
+class Solution {
+public:
+ string longestWord(vector& words) {
+ unordered_set s(words.begin(), words.end());
+ ranges::sort(words, [&](const string& a, const string& b) {
+ return a.size() > b.size() || (a.size() == b.size() && a < b);
+ });
+ auto dfs = [&](this auto&& dfs, string w) -> bool {
+ if (w.empty()) {
+ return true;
+ }
+ for (int k = 1; k <= w.size(); ++k) {
+ if (s.contains(w.substr(0, k)) && dfs(w.substr(k))) {
+ return true;
+ }
+ }
+ return false;
+ };
+ for (const string& w : words) {
+ s.erase(w);
+ if (dfs(w)) {
+ return w;
+ }
+ }
+ return "";
+ }
+};
+```
-func (this *Trie) search(word string) bool {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- return false
- }
- node = node.children[c]
- }
- return node.isEnd
-}
+#### Go
+```go
func longestWord(words []string) string {
+ s := map[string]bool{}
+ for _, w := range words {
+ s[w] = true
+ }
sort.Slice(words, func(i, j int) bool {
- a, b := words[i], words[j]
- if len(a) != len(b) {
- return len(a) < len(b)
- }
- return a > b
+ return len(words[i]) > len(words[j]) || (len(words[i]) == len(words[j]) && words[i] < words[j])
})
- trie := newTrie()
var dfs func(string) bool
dfs = func(w string) bool {
if len(w) == 0 {
return true
}
- for i := 1; i <= len(w); i++ {
- if trie.search(w[:i]) && dfs(w[i:]) {
+ for k := 1; k <= len(w); k++ {
+ if s[w[:k]] && dfs(w[k:]) {
return true
}
}
return false
}
- ans := ""
for _, w := range words {
+ s[w] = false
if dfs(w) {
- ans = w
+ return w
}
- trie.insert(w)
}
- return ans
+ return ""
}
```
-#### Swift
+#### TypeScript
-```swift
-class Trie {
- var children = [Trie?](repeating: nil, count: 26)
- var isEnd = false
-
- func insert(_ word: String) {
- var node = self
- for ch in word {
- let index = Int(ch.asciiValue! - Character("a").asciiValue!)
- if node.children[index] == nil {
- node.children[index] = Trie()
+```ts
+function longestWord(words: string[]): string {
+ const s = new Set(words);
+
+ words.sort((a, b) => (a.length === b.length ? a.localeCompare(b) : b.length - a.length));
+
+ const dfs = (w: string): boolean => {
+ if (w === '') {
+ return true;
+ }
+ for (let k = 1; k <= w.length; ++k) {
+ if (s.has(w.substring(0, k)) && dfs(w.substring(k))) {
+ return true;
}
- node = node.children[index]!
}
- node.isEnd = true
+ return false;
+ };
+
+ for (const w of words) {
+ s.delete(w);
+ if (dfs(w)) {
+ return w;
+ }
}
- func search(_ word: String) -> Bool {
- var node = self
- for ch in word {
- let index = Int(ch.asciiValue! - Character("a").asciiValue!)
- if node.children[index] == nil {
- return false
+ return '';
+}
+```
+
+#### Rust
+
+```rust
+use std::collections::HashSet;
+
+impl Solution {
+ pub fn longest_word(words: Vec) -> String {
+ let mut s: HashSet = words.iter().cloned().collect();
+ let mut words = words;
+ words.sort_by(|a, b| b.len().cmp(&a.len()).then(a.cmp(b)));
+
+ fn dfs(w: String, s: &mut HashSet) -> bool {
+ if w.is_empty() {
+ return true;
}
- node = node.children[index]!
+ for k in 1..=w.len() {
+ if s.contains(&w[0..k]) && dfs(w[k..].to_string(), s) {
+ return true;
+ }
+ }
+ false
}
- return node.isEnd
+ for w in words {
+ s.remove(&w);
+ if dfs(w.clone(), &mut s) {
+ return w;
+ }
+ }
+ String::new()
}
}
+```
+
+#### Swift
+```swift
class Solution {
func longestWord(_ words: [String]) -> String {
- var words = words.sorted(by: { $0.count < $1.count || ($0.count == $1.count && $0 > $1) })
- let trie = Trie()
+ var s: Set = Set(words)
+ var words = words
+ words.sort { (a, b) -> Bool in
+ if a.count == b.count {
+ return a < b
+ } else {
+ return a.count > b.count
+ }
+ }
- var dfs: ((String) -> Bool)!
- dfs = { w in
+ func dfs(_ w: String) -> Bool {
if w.isEmpty {
return true
}
- for i in 1...w.count {
- if trie.search(String(w.prefix(i))) && dfs(String(w.suffix(w.count - i))) {
+ for k in 1...w.count {
+ let prefix = String(w.prefix(k))
+ if s.contains(prefix) && dfs(String(w.dropFirst(k))) {
return true
}
}
return false
}
- var ans = ""
for w in words {
+ s.remove(w)
if dfs(w) {
- ans = w
+ return w
}
- trie.insert(w)
}
- return ans
+
+ return ""
}
}
```
diff --git a/lcci/17.15.Longest Word/Solution.cpp b/lcci/17.15.Longest Word/Solution.cpp
new file mode 100644
index 0000000000000..19b158b023c7d
--- /dev/null
+++ b/lcci/17.15.Longest Word/Solution.cpp
@@ -0,0 +1,27 @@
+class Solution {
+public:
+ string longestWord(vector& words) {
+ unordered_set s(words.begin(), words.end());
+ ranges::sort(words, [&](const string& a, const string& b) {
+ return a.size() > b.size() || (a.size() == b.size() && a < b);
+ });
+ auto dfs = [&](this auto&& dfs, string w) -> bool {
+ if (w.empty()) {
+ return true;
+ }
+ for (int k = 1; k <= w.size(); ++k) {
+ if (s.contains(w.substr(0, k)) && dfs(w.substr(k))) {
+ return true;
+ }
+ }
+ return false;
+ };
+ for (const string& w : words) {
+ s.erase(w);
+ if (dfs(w)) {
+ return w;
+ }
+ }
+ return "";
+ }
+};
diff --git a/lcci/17.15.Longest Word/Solution.go b/lcci/17.15.Longest Word/Solution.go
index 2a6dbd07cf5a4..321fb05d318ae 100644
--- a/lcci/17.15.Longest Word/Solution.go
+++ b/lcci/17.15.Longest Word/Solution.go
@@ -1,62 +1,28 @@
-type Trie struct {
- children [26]*Trie
- isEnd bool
-}
-
-func newTrie() *Trie {
- return &Trie{}
-}
-func (this *Trie) insert(word string) {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- node.children[c] = newTrie()
- }
- node = node.children[c]
- }
- node.isEnd = true
-}
-
-func (this *Trie) search(word string) bool {
- node := this
- for _, c := range word {
- c -= 'a'
- if node.children[c] == nil {
- return false
- }
- node = node.children[c]
- }
- return node.isEnd
-}
-
func longestWord(words []string) string {
+ s := map[string]bool{}
+ for _, w := range words {
+ s[w] = true
+ }
sort.Slice(words, func(i, j int) bool {
- a, b := words[i], words[j]
- if len(a) != len(b) {
- return len(a) < len(b)
- }
- return a > b
+ return len(words[i]) > len(words[j]) || (len(words[i]) == len(words[j]) && words[i] < words[j])
})
- trie := newTrie()
var dfs func(string) bool
dfs = func(w string) bool {
if len(w) == 0 {
return true
}
- for i := 1; i <= len(w); i++ {
- if trie.search(w[:i]) && dfs(w[i:]) {
+ for k := 1; k <= len(w); k++ {
+ if s[w[:k]] && dfs(w[k:]) {
return true
}
}
return false
}
- ans := ""
for _, w := range words {
+ s[w] = false
if dfs(w) {
- ans = w
+ return w
}
- trie.insert(w)
}
- return ans
-}
\ No newline at end of file
+ return ""
+}
diff --git a/lcci/17.15.Longest Word/Solution.java b/lcci/17.15.Longest Word/Solution.java
index 2d6e2d40bd4c3..e7bbcc380a592 100644
--- a/lcci/17.15.Longest Word/Solution.java
+++ b/lcci/17.15.Longest Word/Solution.java
@@ -1,61 +1,34 @@
-class Trie {
- Trie[] children = new Trie[26];
- boolean isEnd;
-
- void insert(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- node.children[c] = new Trie();
- }
- node = node.children[c];
- }
- node.isEnd = true;
- }
-
- boolean search(String word) {
- Trie node = this;
- for (char c : word.toCharArray()) {
- c -= 'a';
- if (node.children[c] == null) {
- return false;
- }
- node = node.children[c];
- }
- return node.isEnd;
- }
-}
-
class Solution {
- private Trie trie = new Trie();
+ private Set