LeetCode --- Valid Palindrome
判断字符串是否为回文串。
附上代码:
1 class Solution {
2 public:
3 bool isPalindrome(string s) {
4 if (s.empty()) return true; // consider empty string as valid palindrome
5 unsigned int len = s.size();
6 unsigned int beg = 0, end = len - 1;
7 while (beg < end) {
8 while (beg <= end && !isdigit(s[beg]) && !isalpha(s[beg]))
9 beg++;
10 while (end >= beg && !isdigit(s[end]) && !isalpha(s[end]))
11 end--;
12 //if (beg == end) return true;
13 if (beg >= end) return true;
14 if (isalpha(s[beg]) && isalpha(s[end])) {
15 if (toupper(s[beg]) != toupper(s[end]))
16 return false;
17 } else if (s[beg] != s[end]) {
18 return false;
19 }
20 beg++;
21 end--;
22 }
23
24 return true;
25 }
26 };
posted on 2014-05-29 21:54 Stomach_ache 阅读(130) 评论(0) 编辑 收藏 举报