算法: 验证回文680. Valid Palindrome II
Posted 架构师易筋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法: 验证回文680. Valid Palindrome II相关的知识,希望对你有一定的参考价值。
680. Valid Palindrome II
Given a string s, return true if the s can be palindrome after deleting at most one character from it.
Example 1:
Input: s = "aba"
Output: true
Example 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
Example 3:
Input: s = "abc"
Output: false
Constraints:
- 1 <= s.length <= 105
- s consists of lowercase English letters.
头尾指针解法
class Solution {
public boolean validPalindrome(String s) {
char[] chars = s.toCharArray();
int lo = 0, hi = chars.length - 1;
while (lo < hi) {
if (chars[lo] != chars[hi]) {
return helper(chars, lo + 1, hi) || helper(chars, lo, hi - 1);
}
lo++;
hi--;
}
return true;
}
private boolean helper(char[] chars, int lo, int hi) {
while (lo < hi) {
if (chars[lo] != chars[hi]) {
return false;
}
lo++;
hi--;
}
return true;
}
}
以上是关于算法: 验证回文680. Valid Palindrome II的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 680. 验证回文字符串 Ⅱ [Valid Palindrome II (Easy)]
LeetCode 680. 验证回文字符串 Ⅱ [Valid Palindrome II (Easy)]