-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.cpp
More file actions
62 lines (57 loc) · 1.34 KB
/
125.cpp
File metadata and controls
62 lines (57 loc) · 1.34 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
//
// 125.cpp
// LeetCode
//
// Created by 张佐玮 on 15/5/16.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Valid Palindrome
//
#include <iostream>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
int front = 0, rear = (int) s.length() - 1;
while (front < rear) {
if (!isLegal(s[front])) {
front++;
}
else if (!isLegal(s[rear])) {
rear--;
}
else if (tolower(s[front]) == tolower(s[rear])) {
front++; rear--;
}
else {
return false;
}
}
return true;
}
private:
static inline bool isLegal(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
}
};
class Test {
private:
static void runTest(string s) {
Solution solution;
bool result = solution.isPalindrome(s);
cout << s << ": " << result << endl;
}
public:
void sample() {
string s1 = "A man, a plan, a canal: Panama";
string s2 = "race a car";
string s3 = "";
string s4 = " ";
string s5 = "1 1";
runTest(s1);
runTest(s2);
runTest(s3);
runTest(s4);
runTest(s5);
}
};