LeetCode Top 100 Liked Questions 5.Longest Palindromic Substring (Java版; Medium)
题目描述
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"
classSolution{public String longestPalindrome(String s){//input checkif(s == null || s.length()<=1){return s;}//记录回文串的左右边界int left =0, right =0;int n = s.length();for(int i =0; i < n; i++){int len1 =expand(s, i, i);int len2 =expand(s, i, i +1);if(len1 > len2 && len1 > right - left +1){
left = i - len1 /2;
right = i + len1 /2;}elseif(len2 > len1 && len2 > right - left +1){
left = i -(len2 -2)/2;
right = i+1+(len2 -2)/2;}}return s.substring(left, right +1);}privateintexpand(String s,int x,int y){int left = x, right = y;while(x >=0&& y < s.length()&& s.charAt(x)== s.charAt(y)){
x--;
y++;}return y - x -1;}}
第一次做, 统一长度的更新公式; 调整了for循环条件, while循环条件
classSolution{public String longestPalindrome(String s){if(s==null || s.length()<2)return s;int left =0, right =0;int len1 =0, len2 =0;//比起上一次修改了i的终止条件for(int i=0; i<s.length(); i++){
len1 =centerExpand(s, i, i);
len2 =centerExpand(s, i, i+1);
len1 = Math.max(len1, len2);if(len1 > right - left +1){
left = i -(len1 -1)/2;
right = i + len1/2;}}return s.substring(left, right+1);}publicintcenterExpand(String s,int i,int j){int left=i, right=j;while(left>=0&& right<s.length()&& s.charAt(left)==s.charAt(right)){
left--;
right++;}return right - left -1;}}
第一次做, 使用中心扩展法
中心可能是同一个字符, 也可能是相邻的两个字符;
字符串长度小于2时直接返回;
注意循环终止条件, s至少有两个字符才会进入循环
长度一次增加2
classSolution{public String longestPalindrome(String s){//字符串长度小于2时直接返回if(s==null || s.length()<2)return s;
String curr ="";int len1, len2;int left, right;//注意循环终止条件, s至少有两个字符才会进入循环for(int i=0; i<s.length()-1; i++){
len1 =centerExpand(s, i, i);
len2 =centerExpand(s, i, i+1);if(len1 > len2){
left = i -(len1 -1)/2;
right = i +(len1 -1)/2;//可以写成i + len1/2
curr = len1 > curr.length()? s.substring(left, right+1): curr;}else{
left = i -(len2-2)/2;//可以写成i-(len2-1)/2 和一个中心点的情况统一了
right = i +1+(len2-2)/2;//可以写成i + len2/2
curr = len2 > curr.length()? s.substring(left, right+1): curr;}}return curr;}publicintcenterExpand(String s,int i,int j){// 相邻两个字符不同, 直接返回0if(s.charAt(i)!= s.charAt(j))return0;int len = j - i +1;int step=1;while((i-step)>=0&&(j+step)<s.length()){if(s.charAt(i-step)== s.charAt(j+step)){//长度一次加2!
len +=2;
step++;}elsebreak;}return len;}}
题解中心扩展法
public String longestPalindrome(String s){if(s == null || s.length()<1)return"";int start =0, end =0;for(int i =0; i < s.length(); i++){int len1 =expandAroundCenter(s, i, i);int len2 =expandAroundCenter(s, i, i +1);int len = Math.max(len1, len2);if(len > end - start){
start = i -(len -1)/2;
end = i + len /2;}}return s.substring(start, end +1);}privateintexpandAroundCenter(String s,int left,int right){int L = left, R = right;while(L >=0&& R < s.length()&& s.charAt(L)== s.charAt(R)){
L--;
R++;}return R - L -1;}
LeetCode Top 100 Liked Questions 5.Longest Palindromic Substring (Java版; Medium)
welcome to my blogLeetCode Top 100 Liked Questions 4.Longest Palindromic Substring (Java版; Medium)题目描述Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...