Skip to content

Commit 327791a

Browse files
committed
通过较容易,不明白为啥21%的率
1 parent acf2a52 commit 327791a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

string/Valid Palindrome

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 想要学会 isalnum 的用法,但是。。
2+
3+
class Solution {
4+
private:
5+
int checkAlpha(char c)
6+
{
7+
if(c >= '0' && c <='9')
8+
return c;
9+
if(c >= 'A' &&c <= 'Z' )
10+
return c - 'A';
11+
if(c >= 'a' && c <= 'z')
12+
return c - 'a';
13+
return -1;
14+
}
15+
16+
public:
17+
bool isPalindrome(string s) {
18+
for(int i = 0,j = s.size() - 1; j - i >= 1;)
19+
{
20+
int valueA = checkAlpha(s[i]);
21+
int valueB = checkAlpha(s[j]);
22+
if (valueA == valueB)
23+
{
24+
++i;--j;
25+
}
26+
else
27+
{
28+
if(valueA >= 0 &valueB >= 0)
29+
return false;
30+
else
31+
if(valueA <0) ++i;
32+
else
33+
if(valueB < 0) --j;
34+
}
35+
}
36+
return true;
37+
}
38+
};

0 commit comments

Comments
 (0)