标签:mamicode lin solution tostring end long ++ img turn
1 class Solution { 2 public String longestPalindrome(String s) { 3 char[] ch = s.toCharArray(); 4 if(ch.length==0){ 5 return ""; 6 } 7 int left = 0; 8 int right = 0; 9 int index = 0; 10 int j =0; 11 int k=0; 12 for(int i=1;i<ch.length;i++){ 13 while(index<i){ 14 for(j=index,k=0;j<=i-k;j++,k++){ 15 if(ch[j] != ch[i-k]){ 16 break; 17 } 18 } 19 if(j>=i-k){ 20 if(i-index>right-left) { 21 left = index; 22 right = i; 23 } 24 } 25 index++; 26 } 27 index = 0; 28 } 29 30 StringBuffer stringBuffer = new StringBuffer(); 31 while(left<=right) { 32 stringBuffer.append(ch[left]); 33 left++; 34 } 35 return stringBuffer.toString(); 36 } 37 }
很明显,耗时太严重,现在考虑如何优化??
标签:mamicode lin solution tostring end long ++ img turn
原文地址:https://www.cnblogs.com/baizhuang/p/11419967.html