-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPalindromicSubstring.java
47 lines (47 loc) · 1.22 KB
/
LongestPalindromicSubstring.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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.
* 查找O(N*2)
* @author sinlly
*
*/
public class LongestPalindromicSubstring {
public String longestPalindrome(String s) {
if(s==null)
return null;
if(s.length()<=1)
return s;
int len=s.length();
String maxStr=s.substring(0,1);
for(int i=0;i<len;i++)
{
//回文子串只有一个字母
String str=findStr(s,i,i);
if(str.length()>maxStr.length())
maxStr=str;
str=findStr(s,i,i+1);
if(str.length()>maxStr.length())
maxStr=str;
}
return maxStr;
}
public String findStr(String s,int start,int end)
{
while(start>=0 && end<s.length() && s.charAt(start)==s.charAt(end))
{
start--;
end++;
}
return s.substring(start+1,end);//返回回文子串
}
public static void main(String[] args)
{
LongestPalindromicSubstring str=new LongestPalindromicSubstring();
// StringBuffer buf=new StringBuffer();
// for(int i=0;i<1000;i++)
// buf.append("a");
//
// System.out.println( str.longestPalindrome(buf.toString()));
System.out.println(str.longestPalindrome("abcddcbaababa"));
}
}