是要把每一个位置当做子串的中心位置,从中心向左右扩展
class Solution {
private int left;
private int len;
public String longestPalindrome(String s) {
if(s.length()<2 || s == null){
return s;
}
// int left = 0;
// int right = 0;
// int len = 0;
// int maxStart = 0;
// int maxLen = 0;
//中心扩展法
for(int i=0; i<s.length(); i++){//把每一个位置当做中心,寻找以其为中心的最大回文子串
center_expend(s, i, i);//奇数
center_expend(s, i, i+1);//偶数
}
return s.substring(left, left+len);
}
public void center_expend(String s, int left, int right){
//中心扩展法
// int len = 0;
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){
if(right-left+1 > len){
len = right-left+1;
this.left = left;
}
right++;
left--;
}
}
}
判断一个数是否为回文数
class Solution {
public boolean isPalindrome(int x) {
int count = 0;
int num = x;
if(x<0){
return false;
}
while(num!=0){
count = count*10 + num%10;
num /= 10;
}
return count == x;
}
}