public int getLongestPalindrome(String A, int n) { // write code here int res = 1; //以每个点作为中心(奇数个为中心) 求回文子串 for(int i=0;i<A.length();i++){ int temp=getLen(i,A); //System.out.println(temp); res = Math.max(res,temp); } return res; } private int getLen(int index,String A){ //if(index==6) int max = 1; //偶数 max = Math.max(l(index,A,0),max); if(index>0){ //奇数 max = Math.max(l(index,A,1)+1,max); } return max; } private int l(int index,String A,int flag){ if(index+1>=A.length()) return 1; int left = index; //奇数个的时候需要-1 if(flag==1){ left = index-1; } int right = index+1; int i=0; int cnt=0; while(left-i>=0 && (right+i)<A.length()){ if(A.charAt(left-i)==A.charAt(right+i)){ i++; cnt++; }else break; } return cnt*2; }