680. Valid Palindrome II
2017-09-28 15:54
375 查看
Given a non-empty string
Example 1:
Example 2:
Note:
The string will only contain lowercase characters a-z.The maximum length of the string is 50000.
最多只能删除一个字符,问给出的字符串操作后是否为回文字符串。用深度为1的深度优先搜索实现。
代码:
class Solution {
public:
bool validPalindrome(string s) {
if(s.size() <= 2) return true;
return helper(s, 0, s.size()-1, 1);
}
private:
bool helper(const string &s, int l, int r, int cnt) {
while(l <= r-1) {
if(s[l] != s[r]) {
if(cnt <= 0) return false;
return helper(s, l+1, r, cnt-1) || helper(s, l, r-1, cnt-1);
}
l++; r--;
}
return true;
}
};
s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba" Output: True
Example 2:
Input: "abca" Output: True Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z.The maximum length of the string is 50000.
最多只能删除一个字符,问给出的字符串操作后是否为回文字符串。用深度为1的深度优先搜索实现。
代码:
class Solution {
public:
bool validPalindrome(string s) {
if(s.size() <= 2) return true;
return helper(s, 0, s.size()-1, 1);
}
private:
bool helper(const string &s, int l, int r, int cnt) {
while(l <= r-1) {
if(s[l] != s[r]) {
if(cnt <= 0) return false;
return helper(s, l+1, r, cnt-1) || helper(s, l, r-1, cnt-1);
}
l++; r--;
}
return true;
}
};
相关文章推荐
- 680. Valid Palindrome II
- Leetcode算法学习日志-680 valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- 680. Valid Palindrome II
- leetcode 680 Valid Palindrome II
- Leetcode 680 Valid Palindrome II
- 680. Valid Palindrome II
- String-680-Valid Palindrome II
- Leetcode-680. Valid Palindrome II(判断回文)
- Valid Palindrome II
- Valid Palindrome II-面试题
- LeetCode Valid Palindrome II
- leetcode -day13 Valid Palindrome & Triangle & Pascal's Triangle I II
- Valid Palindrome II
- Valid Palindrome II
- Valid Palindrome II
- 【leetcode】680. Valid Palindrome II(Python & C++)
- LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)