[LeetCode125] Valid Palindrome

题目:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

分类:String Two Pointers

代码:

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         string str;
 5         for (auto c : s)
 6         {
 7             if (isalpha(c))
 8             {
 9                 str += tolower(c);
10             }
11             else if (isdigit(c))
12             {
13                 str += c;
14             }
15         }
16 
17         
18         for(int left = 0, right = str.size() - 1; left < right; ++left, --right)
19         {
20             if(str[left] != str[right])
21                 return false;
22         }
23         return true;
24     }
25 };

 

posted @ 2016-08-10 16:48  zhangbaochong  阅读(153)  评论(0编辑  收藏  举报