标签:
1 public String longestPalindrome(String s) { 2 if (s == null || s.length() <=1) 3 return s; 4 5 int start = 0; 6 int longest = 0; 7 for (int i = 0; i < s.length();){ 8 int p1 = i; 9 int p2 = i; 10 while(p2<s.length()-1 && s.charAt(p2) == s.charAt(p2+1)){ 11 p2 ++; 12 } 13 i = p2 + 1; 14 while(p1>0 && p2<s.length()-1 && s.charAt(p1-1) == s.charAt(p2+1)){ 15 p1--; 16 p2++; 17 } 18 int len = p2 - p1 + 1; 19 if (len > longest){ 20 start = p1; 21 longest = len; 22 } 23 } 24 return s.substring(start, start + longest); 25 }
标签:
原文地址:http://www.cnblogs.com/timoBlog/p/4759093.html