题解 | 最长回文子串
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
import java.util.*;
//中心扩散法,很好理解,相比动态规划
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A string字符串
* @return int整型
*/
public int getLongestPalindrome (String A) {
// write code here
int max1 = 0;
int max2 = 0;
for(int i=0;i<A.length();i++){
max1 = max1<sublen(A,i,i)?sublen(A,i,i):max1;
}
for(int i=0;i<A.length();i++){
max2 = max2<sublen(A,i,i+1)?sublen(A,i,i+1):max2;
}
return max1<=max2?max2:max1;
}
public Integer sublen(String s,int l,int r){
while(l>=0 && r<s.length() && s.charAt(l)==s.charAt(r)){
l--;
r++;
}
return r-l-1;
}
}

