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"
class Solution { private String res = "";//res全局变量 public String longestPalindrome(String s) { if (s.length() <= 1) return s; for (int i = 0; i < s.length(); i++){ midExpansion(s, i, i); //for普通(aba) midExpansion(s, i , i + 1); //for至少连续两个(aa) } return res; } public void midExpansion(String s, int left, int right){ while (right < s.length() && left >= 0 && s.charAt(left) == s.charAt(right)){ left--; right++; } if (right - left > res.length()){ res = s.substring(left+1, right); //比之前的长就更新,最后一次更新完left比预期小1 } } }