标签:
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
public String longestPalindrome(String s) {
int tempLen = 0;
String returnStr = null;
for(int i=0;i<s.length()-1;i++){
for (int j=i+1;j<s.length();j++){
String tempStr = s.substring(i, j);
if(isPalindromicString(tempStr)){
if(tempStr.length()>tempLen){
tempLen = tempStr.length();
returnStr = tempStr;
}
}
}
}
System.out.println(tempLen);
return returnStr;
}
/**
* 判断一个字符串是否为回文
* @param str
* @return
*/
private boolean isPalindromicString(String str){
int len = str.length(),len2=str.length()/2;
len2 = len%2==1?len2+1:len2;
for(int i=0;i<len2;i++){
if(str.charAt(i)!=str.charAt(len-1-i)){
return false;
}
}
return true;
}
5:Longest Palindromic Substring
标签:
原文地址:http://www.cnblogs.com/shisw/p/4637985.html