代码
package com.zou.d1116;
public class LongestPalindromeSubstring {
public static void main(String[] args) {
String s = "babad";
System.out.println(longestPalindrome(s));
}
public static String longestPalindrome(String s) {
int len = s.length();
String result = "";
for (int i = 0; i < len * 2 - 1; i++) {
int left = i / 2;
int right = left + i % 2;
while (left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
String tmp = s.substring(left, right + 1);
if (tmp.length() > result.length()) {
result = tmp;
}
left--;
right++;
}
}
return result;
}
}
结果
评论区 0