-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125.验证回文串.cpp
55 lines (49 loc) · 1.09 KB
/
125.验证回文串.cpp
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
/*
* @lc app=leetcode.cn id=125 lang=cpp
*
* [125] 验证回文串
*/
// @lc code=start
class Solution {
public:
bool is_target_char(char& ch)
{
if ('A' <= ch && ch <= 'Z')
{
ch = ch - 'A' + 'a';
return true;
}
else if(('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9'))
{
return true;
}
else
{
return false;
}
}
bool isPalindrome(string s) {
for (int front = 0, behind = s.size() - 1; front < behind; )
{
while (front < behind && !is_target_char(s.at(front)))
{
front++;
}
while (front < behind && !is_target_char(s.at(behind)))
{
behind--;
}
if (front < behind && s.at(front) != s.at(behind))
{
return false;
}
else
{
front++;
behind--;
}
}
return true;
}
};
// @lc code=end