版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/RUNRUN2QQ/article/details/84503164
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
Answer:
class Solution {
public String longestPalindrome(String s) {
if (s == null || s.length() < 1) return "";
char[] chars = s.toCharArray();
int start = 0;
int end = 0;
int maxLen = 0;
for (int i = 0; i < chars.length; i++) {
int lenLeft = getLen1(chars,i);
int lemRight = getLen2(chars,i);
int len = Math.max(lenLeft,lemRight);
if(len>maxLen){
maxLen = len;
if(len == lenLeft){
start = i-(len/2);
end = i + (len/2);
}else {
start = i - (len/2 - 1);
end = i+(len/2);
}
}
}
return s.substring(start,end+1);
}
/**
* 以字母为对称中心
* @param chars
* @param mid
* @return
*/
public int getLen1(char[] chars,int mid){
int left = mid;
int right = mid;
while (left >= 0 && right<chars.length && chars[left] == chars[right]){
left--;
right++;
}
left++;
right--;
return right-left+1;
}
/**
* 以字母间为对称点
* @param chars
* @param mid
* @return
*/
public int getLen2(char[] chars,int mid){
int left = mid;
int right = mid+1;
while (left >= 0 && right<chars.length && chars[left] == chars[right]){
left--;
right++;
}
left++;
right--;
return right-left+1;
}
}