Huawei P8 lite 回国能用 3G 4G 吗?# PDA - 掌中宝
d*4
1 楼
Longest Palindromic Substring这一题. 同样的O(n^2)算法,java能过,python不能
过。
python exceeds time limit 了.
The following is java version:
public String longestPalindrome(String s) {
if(s == null || s.length()==0)
return "";
int maxLen = 0;
String res = "";
for(int i=0;i<2*s.length()-1;i++)
{
int left = i/2;
int right = i/2;
if(i%2==1)
right++;
String str = lengthOfPalindrome(s,left,right);
if(maxLen {
maxLen = str.length();
res = str;
}
}
return res;
}
private String lengthOfPalindrome(String s, int left, int right)
{
while(left>=0 && right {
left--;
right++;
}
return s.substring(left+1,right);
}
过。
python exceeds time limit 了.
The following is java version:
public String longestPalindrome(String s) {
if(s == null || s.length()==0)
return "";
int maxLen = 0;
String res = "";
for(int i=0;i<2*s.length()-1;i++)
{
int left = i/2;
int right = i/2;
if(i%2==1)
right++;
String str = lengthOfPalindrome(s,left,right);
if(maxLen
maxLen = str.length();
res = str;
}
}
return res;
}
private String lengthOfPalindrome(String s, int left, int right)
{
while(left>=0 && right
left--;
right++;
}
return s.substring(left+1,right);
}