Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (57 sloc) 1.04 KB
/*
* @lc app=leetcode.cn id=680 lang=cpp
*
* [680] 验证回文字符串 Ⅱ
*
* https://leetcode-cn.com/problems/valid-palindrome-ii/description/
*
* algorithms
* Easy (29.35%)
* Total Accepted: 3.8K
* Total Submissions: 12.7K
* Testcase Example: '"aba"'
*
* 给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。
*
* 示例 1:
*
*
* 输入: "aba"
* 输出: True
*
*
* 示例 2:
*
*
* 输入: "abca"
* 输出: True
* 解释: 你可以删除c字符。
*
*
* 注意:
*
*
* 字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。
*
*
*/
class Solution {
public:
bool validPalindrome(string s) {
int l=0,r=s.size()-1;
while(l<r){
if(s[l]!=s[r])
return helper(s,l,r-1)||helper(s,l+1,r);
l++,r--;
}
return true;
}
bool helper(string s, int l,int r)
{
while(l<r)
{
if(s[l++]!=s[r--]) return false;
}
return true;
}
};
You can’t perform that action at this time.