Valid Palindrome II
程序员文章站
2024-03-06 23:37:08
...
http://www.lintcode.com/zh-cn/problem/valid-palindrome-ii/
public class Solution {
/**
* @param s: a string
* @return: nothing
*/
public boolean validPalindrome(String s) {
// Write your code here
int left = 0;
int right = s.length() - 1;
boolean hasDeal = false;
while (left < right) {
if (s.charAt(left) == s.charAt(right)) {
left++;
right--;
continue;
} else {
if (!hasDeal) {
if (s.charAt(left + 1) == s.charAt(right)) {
left += 2;
right--;
hasDeal = true;
} else if (s.charAt(left) == s.charAt(right - 1)) {
left++;
right -= 2;
hasDeal = true;
} else {
return false;
}
} else {
return false;
}
}
}
return true;
}
}
上一篇: LeetCode125. Valid Palindrome
下一篇: (三)单向循环链表