-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.cpp
More file actions
executable file
·62 lines (58 loc) · 1.42 KB
/
Copy path19.cpp
File metadata and controls
executable file
·62 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
@filename 19.cpp
@author caonan
@date 2022-03-31 16:10:15
@reference 剑指offer专项
@url https://leetcode-cn.com/problems/RQku0D/
@brief 给定一个非空字符串 s,请判断如果 最多 从字符串中删除一个字符能否得到一个回文字符串。
s 由小写英文字母组成
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Solution
{
public:
// todo:用lamada实现递归
bool validPalindrome(string s)
{
int l = 0;
int r = s.length() - 1;
while (l < r) {
if (s[l] != s[r]) {
break;
}
l++;
r--;
}
return r - l <= 1 || isPalindromeStr(s.substr(l + 1, r - l)) || isPalindromeStr(s.substr(l, r - l));
}
private:
bool isPalindromeStr(const string& s)
{
int l = 0;
int r = s.length() - 1;
while (l < r) {
if (s[l++] != s[r--]) {
return false;
}
}
return true;
}
};
int main()
{
Solution s;
assert(s.validPalindrome("aba"));
assert(s.validPalindrome("abca"));
assert(!s.validPalindrome("abc"));
assert(s.validPalindrome(""));
assert(s.validPalindrome("deeee"));
assert(s.validPalindrome("eedeeee"));
assert(s.validPalindrome("aydmda"));
return 0;
}