标签:c style class blog code java
判断字符串是否为回文串。
附上代码:
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 };
LeetCode --- Valid Palindrome,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/Stomach-ache/p/3760099.html