Given a string s
, return the longest
palindromic
substring
in
s
.
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
1 <= s.length <= 1000
s
consist of only digits and English letters.中心拓展
class Solution {
public String longestPalindrome(String s) {
String res = "";
int len = 0;
for(int i = 0; i < s.length();i++){
String left = subString(s, i, i);
if(left.length() > len){
res = left;
len = left.length();
}
String rigth = subString(s, i, i + 1);
if(rigth.length() > len){
res = rigth;
len = rigth.length();
}
}
return res;
}
String subString(String s, int start, int end){
while(start >= 0 && end < s.length()){
if(s.charAt(start) != s.charAt(end)){
break;
}
start--;
end ++;
}
return s.substring(start + 1, end);
}
}
class Solution {
public String longestPalindrome(String s) {
String res = "";
int len = 0;
for(int i = 0; i < s.length();i++){
String left = subString(s, i, i);
if(left.length() > len){
res = left;
len = left.length();
}
String rigth = subString(s, i, i + 1);
if(rigth.length() > len){
res = rigth;
len = rigth.length();
}
}
return res;
}
String subString(String s, int start, int end){
while(start >= 0 && end < s.length()){
if(s.charAt(start) != s.charAt(end)){
break;
}
start--;
end ++;
}
return s.substring(start + 1, end);
}
}
class Solution {
public String longestPalindrome(String s) {
String res = "";
int len = 0;
for(int i = 0; i < s.length();i++){
String left = subString(s, i, i);
if(left.length() > len){
res = left;
len = left.length();
}
String rigth = subString(s, i, i + 1);
if(rigth.length() > len){
res = rigth;
len = rigth.length();
}
}
return res;
}
String subString(String s, int start, int end){
while(start >= 0 && end < s.length()){
if(s.charAt(start) != s.charAt(end)){
break;
}
start--;
end ++;
}
return s.substring(start + 1, end);
}
}