标签:length r++ substring mic ges turn str1 longest public
Brutal Force: O(n^2)
public class Solution {
public String longestPalindrome(String s) {
String maxPal="";
for(int i=0;i<s.length();i++)
{
String str1=lenofPal(i, i, s);
if(str1.length()>maxPal.length())
maxPal=str1;
String str2=lenofPal(i, i+1, s);
if(str2.length()>maxPal.length())
maxPal=str2;
}
return maxPal;
}
private String lenofPal(int l, int r, String s)
{
while(l>=0&&r<s.length()&&s.charAt(l)==s.charAt(r))
{
l--;
r++;
}
return s.substring(l+1,r);
}
}
Manacher: O(n)
5. Longest Palindromic Substring
标签:length r++ substring mic ges turn str1 longest public
原文地址:http://www.cnblogs.com/asuran/p/7568701.html