先贴题目
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example:
Input: “cbbd”
Output: “bb”
就是要找一个字符串里的回文串
第一种思路先写一个方法判断字符串是否是回文串,如果不是再将子串迭代一下,但这种情况的话”babadada”,如果第二个子串也是回文串则无法获取
class Solution {
public String longestPalindrome(String s) {
int lastindex=0;
String str = null;
for (int i=0;i<s.length();i++){
lastindex=s.lastIndexOf(s.charAt(i));
if(lastindex!=-1&&i!=lastindex){
//说明存在
str = s.substring(i,lastindex+1);
if(palindrome(str)){
//表明是回文串
return str;
}else{
do{
//字符串中的字符串再搜一下
lastindex= s.lastIndexOf(s.charAt(i),s.length()-2);
if(lastindex!=-1){
str = s.substring(i,lastindex+1);
return longestPalindrome(str);
}
}while(s.lastIndexOf(s.charAt(i))==i);
}
}
}
return s.substring(0,1);
}
//判断是否回文串
private boolean palindrome(String s){
for (int i=0;i<s.length()/2;i++){
if(s.toCharArray()[i]!=s.toCharArray()[s.length()-i-1]){
return false;
}
}
return true;
}
}
加了一个最大判断,当resultSize最大时获取result。”babadada”这个算是通过了。但是”aaabaaaa”又不行了。一看在else里面迭代的时候resultSize会重置,之前的resultSize就取不到了。
class Solution {
public String longestPalindrome(String s) {
int lastindex=0;
String str = null;
String result = s.substring(0,1);
int resultSize = 0;
for (int i=0;i<s.length();i++){
lastindex=s.lastIndexOf(s.charAt(i));
if(lastindex!=-1&&i!=lastindex){
//说明存在
str = s.substring(i,lastindex+1);
if(palindrome(str)){
//表明是回文串
if(str.length()>resultSize){
result=str;
resultSize = str.length();
}
}else{
do{
//字符串中的字符串再搜一下
lastindex= s.lastIndexOf(s.charAt(i),s.length()-2);
if(lastindex!=-1){
str = s.substring(i,lastindex+1);
longestPalindrome(str);
}
}while(s.lastIndexOf(s.charAt(i))==i);
}
}
}
return result;
}
//判断是否回文串
private boolean palindrome(String s){
for (int i=0;i<s.length()/2;i++){
if(s.toCharArray()[i]!=s.toCharArray()[s.length()-i-1]){
return false;
}
}
return true;
}
}
又加了一个子方法,迭代子方法。终于可以通过了。
class Solution {
public String longestPalindrome(String s) {
String result = s.substring(0,1);
int resultSize = 0;
return longpal(s,resultSize,result);
}
private String longpal(String s,int resultSize,String result){
String str = null;
int lastindex=0;
for (int i=0;i<s.length();i++){
lastindex = s.lastIndexOf(s.charAt(i));
if (lastindex != -1 && i != lastindex) {
//说明存在
str = s.substring(i, lastindex + 1);
if (palindrome(str)) {
//表明是回文串
if (str.length() > resultSize) {
result = str;
resultSize = str.length();
}
} else {
//字符串长度-1去掉相同的重新查找
str = s.substring(0, s.length() - 1);
lastindex = str.lastIndexOf(s.charAt(i));
if (lastindex != -1 && i != lastindex) {
str = str.substring(i, lastindex + 1);
str=longpal(str,resultSize,result);
if (str.length() > resultSize) {
result = str;
resultSize = str.length();
}
}
}
}
}
return result;
}
//判断是否回文串
private boolean palindrome(String s){
for (int i=0;i<s.length()/2;i++){
if(s.toCharArray()[i]!=s.toCharArray()[s.length()-i-1]){
return false;
}
}
return true;
}
}
但是又给了我下面这个字符串让我找子串,我的方法耗时太长!@#¥%,你nb。想不出来了 看下答案
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
正确答案的思路与我不同,我是从两头向中间判断,答案是从中间向两头判断。这样的话就不用判断子串是否符合条件 不需要迭代 减少了时间复杂度。
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}
答案的时间复杂度为O(n2).空间复杂度为O(1)
判断两种情况如“abba”,”abbcbba”比较大小。