refactor: remove Java code sections from all LeetCode Hot 100 markdown files
- Remove all "## Java 解法" sections and Java code blocks - Replace "## Go 解法" with "## 解法" - Remove "### Go 代码要点" and "### Java 代码要点" sections - Keep all Go code sections intact - Maintain complete documentation structure and content - Update 22 markdown files in the LeetCode Hot 100 directory
This commit is contained in:
@@ -139,72 +139,6 @@ func main() {
|
||||
|
||||
### Java 实现(回溯法)
|
||||
|
||||
```java
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateParentheses {
|
||||
|
||||
public List<String> generateParenthesis(int n) {
|
||||
List<String> result = new ArrayList<>();
|
||||
StringBuilder current = new StringBuilder();
|
||||
backtrack(result, current, 0, 0, n);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void backtrack(List<String> result, StringBuilder current,
|
||||
int open, int close, int max) {
|
||||
// 终止条件:生成了 2n 个括号
|
||||
if (current.length() == 2 * max) {
|
||||
result.add(current.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
// 添加左括号:左括号数量小于 n
|
||||
if (open < max) {
|
||||
current.append('(');
|
||||
backtrack(result, current, open + 1, close, max);
|
||||
current.deleteCharAt(current.length() - 1); // 回溯
|
||||
}
|
||||
|
||||
// 添加右括号:右括号数量小于左括号数量
|
||||
if (close < open) {
|
||||
current.append(')');
|
||||
backtrack(result, current, open, close + 1, max);
|
||||
current.deleteCharAt(current.length() - 1); // 回溯
|
||||
}
|
||||
}
|
||||
|
||||
// 测试用例
|
||||
public static void main(String[] args) {
|
||||
GenerateParentheses solution = new GenerateParentheses();
|
||||
|
||||
// 测试用例1
|
||||
int n1 = 3;
|
||||
System.out.println("输入: n = " + n1);
|
||||
System.out.println("输出: " + solution.generateParenthesis(n1));
|
||||
|
||||
// 测试用例2
|
||||
int n2 = 1;
|
||||
System.out.println("\n输入: n = " + n2);
|
||||
System.out.println("输出: " + solution.generateParenthesis(n2));
|
||||
|
||||
// 测试用例3
|
||||
int n3 = 4;
|
||||
System.out.println("\n输入: n = " + n3);
|
||||
List<String> result3 = solution.generateParenthesis(n3);
|
||||
System.out.println("输出长度: " + result3.size());
|
||||
System.out.println("输出: " + result3);
|
||||
|
||||
// 验证卡特兰数
|
||||
System.out.println("\n卡特兰数验证:");
|
||||
for (int i = 1; i <= 8; i++) {
|
||||
System.out.println("n = " + i + ", 组合数 = " +
|
||||
solution.generateParenthesis(i).size());
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Go 实现(动态规划)
|
||||
|
||||
@@ -234,30 +168,6 @@ func generateParenthesisDP(n int) []string {
|
||||
|
||||
### Java 实现(动态规划)
|
||||
|
||||
```java
|
||||
public List<String> generateParenthesisDP(int n) {
|
||||
List<List<String>> dp = new ArrayList<>();
|
||||
List<String> dp0 = new ArrayList<>();
|
||||
dp0.add("");
|
||||
dp.add(dp0);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
List<String> current = new ArrayList<>();
|
||||
for (int j = 0; j < i; j++) {
|
||||
List<String> leftList = dp.get(j);
|
||||
List<String> rightList = dp.get(i - 1 - j);
|
||||
for (String left : leftList) {
|
||||
for (String right : rightList) {
|
||||
current.add("(" + left + ")" + right);
|
||||
}
|
||||
}
|
||||
}
|
||||
dp.add(current);
|
||||
}
|
||||
|
||||
return dp.get(n);
|
||||
}
|
||||
```
|
||||
|
||||
## 复杂度分析
|
||||
|
||||
|
||||
Reference in New Issue
Block a user