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.
验证回文与否,这个估计大家学c语言的时候就都写过,感觉我下面写的还是乱了一点:
1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if (s.size() == 0) 5 return true; 6 int end = s.size() - 1; 7 int beg = 0; 8 while (beg < end){ 9 while (!isalpha(s[beg]) && !isdigit(s[beg])){ 10 if(beg < end) 11 beg++; 12 else 13 return true; 14 } 15 while (!isalpha(s[end]) && !isdigit(s[end])){ 16 if(end > beg) 17 end--; 18 else 19 return true; 20 } 21 if (toupper(s[beg]) == toupper(s[end])){ 22 beg++, end--; 23 } 24 else{ 25 return false; 26 } 27 } 28 return true; 29 } 30 };
LeetCode OJ:Valid Palindrome(验证回文)
原文:http://www.cnblogs.com/-wang-cheng/p/4862130.html