Leetcode-680. Valid Palindrome II(判断回文)
2017-10-05 10:37
417 查看
题目大意:给定一个非空字符串s,您最多可以删除一个字符。判断你是否能把它变成一串“回文”。
题目解析:最开始用的暴力,结果超时了,换用递归
解题代码:
class Solution {
public:
bool validPalindrome(string s) {
int len=s.length();
if(len<=2) return true;
return judge(s,0,len-1,1);
}
private:
bool judge(string s,int l,int r,int cnt){
while(l<=r-1){
if(s[l]!=s[r])
{
if(cnt<=0) return false;
return judge(s,l+1,r,cnt-1)||judge(s,l,r-1,cnt-1);
}
l++;r--;
}
return true;
}
};
~step by step
题目解析:最开始用的暴力,结果超时了,换用递归
解题代码:
class Solution {
public:
bool validPalindrome(string s) {
int len=s.length();
if(len<=2) return true;
return judge(s,0,len-1,1);
}
private:
bool judge(string s,int l,int r,int cnt){
while(l<=r-1){
if(s[l]!=s[r])
{
if(cnt<=0) return false;
return judge(s,l+1,r,cnt-1)||judge(s,l,r-1,cnt-1);
}
l++;r--;
}
return true;
}
};
~step by step
相关文章推荐
- LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)
- 【leetcode】680. Valid Palindrome II(Python & C++)
- [LeetCode] Valid Palindrome II 验证回文字符串之二
- valid-palindrome——判断带符号数字字母的字符串是否为回文
- Leetcode 680 Valid Palindrome II
- [分析总结:leetcode-Palindrome Linked List] 给定单链表,判断链表是否为回文。
- [LeetCode] Palindrome Permutation II 回文全排列之二
- leetcode Palindrome Number (判断整数是否为回文)
- LeetCode 132. Palindrome Partitioning II(回文切分)
- leetcode 680 Valid Palindrome II
- LeetCode234_PalindromeLinkedList (判断是否为回文链表) Java题解
- LeetCode-Palindrome Partitioning II-回文分割-DP优化
- LeetCode Valid Palindrome II
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- leetcode -day13 Valid Palindrome & Triangle & Pascal's Triangle I II
- Palindrome Linked List - LeetCode 判断一个链表是否是回文结构
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- Palindrome Number leetcode 判断一个整数是否是回文
- [LeetCode 132] - 回文分割II(Palindrome Partitioning II)
- leetcode(valid-palindrome)判断字符串是否是对称