在 leetcode 中,我试图解决“最长回文子串”任务。这是代码:
public String longestPalindrome(String s) {
String str = "";
for(int i = 0; i < s.length(); i++)
{
for(int j = 1 + i; j < s.length() + 1; j++)
{
String sub = s.substring(i, j);
if(isPalindrome(sub) && sub.length() > str.length())
{
str = s.substring(i, j);
}
}
}
return str;
}
public static boolean isPalindrome(String s)
{
if(s.length() < 2)
return true;
else
for(int i = 0; i < s.length() / 2; i++)
{
if(!(s.charAt(i) == s.charAt(s.length() - 1 - i)))
return false;
}
return true;
}
当我在 Eclipse 中运行它时它可以工作,但是当我想将解决方案提交给 leetcode 时,它显示了一个错误:
Submission Result: Time Limit Exceeded
Last executed input:
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee...
你能告诉我,我有什么问题吗?
慕神8447489
相关分类