-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125.验证回文串.cpp
52 lines (46 loc) · 1.12 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
/*
* @lc app=leetcode.cn id=125 lang=cpp
*
* [125] 验证回文串
*/
// @lc code=start
#include <string>
#include <algorithm>
class Solution {
public:
bool isPalindrome(std::string s) {
if(s.empty()) return true;
auto begin = s.begin();
auto end = s.end() - 1;
while(std::distance(begin, end) > 0){
auto b = std::tolower(*begin);
auto e = std::tolower(*end);
while(!isNumber(b) && !isLetter(b)){
begin++;
b = std::tolower(*begin);
if(begin == end)
return true;
}
while(!isNumber(e) && !isLetter(e)){
end--;
e = std::towlower(*end);
if(begin == end)
return true;
}
if(b != e){
return false;
}
begin++;
end--;
}
return true;
}
private:
bool isNumber(char c) const{
return c >= '0' && c <= '9';
}
bool isLetter(char c) const{
return c <= 'z' && c>= 'a';
}
};
// @lc code=end