标签:public turn targe ble sub 示例 href toc return
示例 1:
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd"
输出:"bb"
示例 3:
输入:s = "a"
输出:"a"
示例 4:
输入:s = "ac"
输出:"a"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
//暴力
class Solution {
public String longestPalindrome(String s) {
int len = s.length();
if(len < 2) return s;
int maxLen = 1;
int begin = 0;
char[] charArray = s.toCharArray();
for(int i=0; i<len-1; i++){
for(int j=i+1; j<len; j++){
if(j-i+1 > maxLen && check(charArray,i,j)){
maxLen = j - i + 1;
begin = i;
}
}
}
return s.substring(begin,begin+maxLen);
}
public boolean check(char[] charArray,int left,int right){
while(left < right){
if(charArray[left] != charArray[right]) return false;
right--;
left++;
}
return true;
}
}
标签:public turn targe ble sub 示例 href toc return
原文地址:https://www.cnblogs.com/xiaofff/p/14398526.html