参考:http://blog.youkuaiyun.com/soszou/article/details/37312317
public class Solution {
public String longestPalindrome(String s) {
int len = s.length();
String ans = "";
String tmp;
for(int i = 0; i < len ; i++){
tmp = solve(s, i , i);
if(tmp.length() > ans.length()){
ans = tmp;
}
if(i + 1 < len){
tmp = solve(s, i, i + 1);
if(tmp.length() > ans.length()){
ans = tmp;
}
}
}
return ans;
}
public String solve(String s, int x, int y){//
int len = s.length();
while(x >= 0 && y < len && s.charAt(x) == s.charAt(y)){
x --;
y ++;
}
return s.substring(x + 1, y);
}
}

本文介绍了一种寻找字符串中最长回文子串的方法。通过定义solve方法来检查并返回以指定位置为中心的最大回文子串,实现了遍历输入字符串并找出最长回文序列的功能。
285

被折叠的 条评论
为什么被折叠?



