5. Longest Palindromic Substring
程序员文章站
2024-03-23 11:36:34
...
题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
public class Solution {
public String longestPalindrome(String s) {
if(s.isEmpty())
return null;
if(s.length()==1)
return s;
int n=s.length();
// String rec=s.substring(0,1);
String max=s.substring(0,1);
for(int i=0;i<n;i++)
{
String rec1=helper(s,i,i+1);
String rec2=helper(s,i,i);
if(!(rec1.length()<max.length()&&rec2.length()<max.length()))
if(rec1.length()>=rec2.length())
max=rec1;
else
max=rec2;
}
return max;
}
public String helper(String s,int begin,int end)
{
while(begin>=0&&end<=s.length()-1&&s.charAt(begin)==s.charAt(end))
{
begin--;
end++;
}
return s.substring(begin+1,end);
}
}
上一篇: MFC_9.21_10:35 KlineMiddleView
下一篇: 5.数据类型-number
推荐阅读
-
5. Longest Palindromic Substring
-
5. Longest Palindromic Substring
-
5. Longest Palindromic Substring
-
5. Longest Palindromic Substring
-
LeetCode 5. Longest Palindromic Substring (DP)
-
5. Longest Palindromic Substring
-
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt
-
LeetCode[字符串] - #3 Longest Substring Without Repeating Characters 博客分类: LeetCode LeetCodeJavaAlgorithm题解String
-
Longest Substring with At Least K Repeating Characters
-
3. Longest Substring Without Repeating Characters 不含重复字母的最长子串