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:
@@ -136,71 +136,6 @@ func main() {
|
||||
|
||||
### Java 实现(中心扩展法)
|
||||
|
||||
```java
|
||||
public class LongestPalindromicSubstring {
|
||||
|
||||
public String longestPalindrome(String s) {
|
||||
if (s == null || s.length() < 2) {
|
||||
return s;
|
||||
}
|
||||
|
||||
int start = 0, maxLen = 1;
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
// 奇数长度:以当前字符为中心
|
||||
int len1 = expandAroundCenter(s, i, i);
|
||||
// 偶数长度:以当前字符和下一个字符之间为中心
|
||||
int len2 = expandAroundCenter(s, i, i + 1);
|
||||
|
||||
int currentLen = Math.max(len1, len2);
|
||||
if (currentLen > maxLen) {
|
||||
maxLen = currentLen;
|
||||
start = i - (currentLen - 1) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
return s.substring(start, start + maxLen);
|
||||
}
|
||||
|
||||
private int expandAroundCenter(String s, int left, int right) {
|
||||
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
|
||||
left--;
|
||||
right++;
|
||||
}
|
||||
return right - left - 1;
|
||||
}
|
||||
|
||||
// 测试用例
|
||||
public static void main(String[] args) {
|
||||
LongestPalindromicSubstring solution = new LongestPalindromicSubstring();
|
||||
|
||||
// 测试用例1
|
||||
String s1 = "babad";
|
||||
System.out.println("输入: " + s1);
|
||||
System.out.println("输出: " + solution.longestPalindrome(s1));
|
||||
|
||||
// 测试用例2
|
||||
String s2 = "cbbd";
|
||||
System.out.println("\n输入: " + s2);
|
||||
System.out.println("输出: " + solution.longestPalindrome(s2));
|
||||
|
||||
// 测试用例3: 单个字符
|
||||
String s3 = "a";
|
||||
System.out.println("\n输入: " + s3);
|
||||
System.out.println("输出: " + solution.longestPalindrome(s3));
|
||||
|
||||
// 测试用例4: 全部相同
|
||||
String s4 = "aaaa";
|
||||
System.out.println("\n输入: " + s4);
|
||||
System.out.println("输出: " + solution.longestPalindrome(s4));
|
||||
|
||||
// 测试用例5: 无回文
|
||||
String s5 = "abc";
|
||||
System.out.println("\n输入: " + s5);
|
||||
System.out.println("输出: " + solution.longestPalindrome(s5));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Go 实现(动态规划)
|
||||
|
||||
@@ -246,41 +181,6 @@ func longestPalindromeDP(s string) string {
|
||||
|
||||
### Java 实现(动态规划)
|
||||
|
||||
```java
|
||||
public String longestPalindromeDP(String s) {
|
||||
if (s == null || s.length() < 2) {
|
||||
return s;
|
||||
}
|
||||
|
||||
int n = s.length();
|
||||
boolean[][] dp = new boolean[n][n];
|
||||
int start = 0, maxLen = 1;
|
||||
|
||||
// 初始化:所有单个字符都是回文串
|
||||
for (int i = 0; i < n; i++) {
|
||||
dp[i][i] = true;
|
||||
}
|
||||
|
||||
// 按长度递增的顺序遍历
|
||||
for (int length = 2; length <= n; length++) {
|
||||
for (int i = 0; i <= n - length; i++) {
|
||||
int j = i + length - 1;
|
||||
|
||||
if (s.charAt(i) == s.charAt(j)) {
|
||||
if (length == 2 || dp[i + 1][j - 1]) {
|
||||
dp[i][j] = true;
|
||||
if (length > maxLen) {
|
||||
maxLen = length;
|
||||
start = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s.substring(start, start + maxLen);
|
||||
}
|
||||
```
|
||||
|
||||
### Go 实现(Manacher 算法)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user