LeetCode: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.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

两个指针首尾判断即可,跳过非字母数字字符,注意大小写,代码如下:                                                                   本文地址

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         int len = s.length();
 7         if(len <= 1)return true;
 8         int i = 0, j = len - 1;
 9         while(i <= j)
10         {
11             if(isAlphanumeric(s[i]) == false)
12                 i++;
13             else if(isAlphanumeric(s[j]) == false)
14                 j--;
15             else 
16             {
17                 if(isSame(s[i], s[j]) == true)
18                     {i++; j--;}
19                 else return false;
20             }
21         }
22         return true;
23     }
24     bool isAlphanumeric(char c)
25     {
26         if((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')||(c >= '0' && c <= '9'))
27             return true;
28         else return false;
29     }
30     bool isSame(char a, char b)
31     {
32         if(a >= 'A' && a <= 'Z')
33             a += 32;
34         if(b >= 'A' && b <= 'Z')
35             b += 32;
36         return a == b;
37     }
38 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3434565.html

原文地址:https://www.cnblogs.com/TenosDoIt/p/3434565.html