每日一道算法面试题（6）：leetcode214 最短回文串

输入: "aacecaaa"



输入: "abcd"



    public String shortestPalindrome(String s) {
if (s == null || s.length() == 0) {
return "";
}
int len = s.length();
int maxLength = 0;
int maxStart = 0;
int maxEnd = 0;
for(int i = 0; i <= len / 2; i ++) {
int findLen1 = expandAroundCenter(s, i, i); // 考虑“aba”的情况
int findLen2 = expandAroundCenter(s, i, i + 1); // 考虑“cbbc”的情况
int findLen = Math.max(findLen1, findLen2);
int finStart = i - (findLen - 1) / 2;
if (findLen > maxLength && finStart == 0) {
maxLength = findLen;
maxStart = 0;
maxEnd = i + findLen / 2;
}
}
if (maxStart == 0 && len > 1) {
} else if (len > 1){
} else {
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = needAdd.length() - 1; i >= 0; i --) {
}
stringBuilder.append(s);
return stringBuilder.toString();
}
return s;
}

// 中心扩展算法：返回最大的回文字符串长度
public int expandAroundCenter(String s, int left, int right) {
int len = s.length();
while (left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
left --;
right ++;
}
if (left < 0 || right >= len || s.charAt(left) != s.charAt(right)) {
left ++;
right --;
}
return right - left + 1;
}


03-10 406
05-17 344

02-02 2591
08-22 3255
05-06 1577
03-10 2523
05-06 195
02-18 556
09-06 471