-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-125.cpp
More file actions
34 lines (31 loc) · 995 Bytes
/
LeetCode-125.cpp
File metadata and controls
34 lines (31 loc) · 995 Bytes
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
/*************************************************************************
> File Name: LeetCode-125.cpp
> Author: ltw
> Mail: 3245849061@qq.com
> Created Time: Tue 19 May 2020 09:03:30 PM CST
************************************************************************/
#include <iostream>
using namespace std;
class Solution {
public:
bool isAlpha(char ch) {
return ch <= 'z' && ch >= 'a';
}
bool isDigit(char ch) {
return ch <= '9' && ch >= '0';
}
bool isPalindrome(string s) {
int p1 = 0, p2 = s.size() - 1;
for (int i = 0; s[i]; i++) {
if (s[i] <= 'Z' && s[i] >= 'A') s[i] += 32;
}
while (p1 < p2) {
while (p1 < s.size() && !isAlpha(s[p1]) && !isDigit(s[p1])) ++p1;
while (p2 >= 0 && !isAlpha(s[p2]) && !isDigit(s[p2])) --p2;
if (p1 >= p2) break;
if (s[p1] != s[p2]) return false;
p1++, p2--;
}
return true;
}
};