125. 验证回文串

 1 class Solution 
 2 {
 3 public:
 4     bool isPalindrome(string s) 
 5     {
 6         string str;
 7         for(int i = 0;i < s.size();i ++)
 8         {
 9             if(isdigit(s[i])) str.push_back(s[i]);
10             if(isalpha(s[i])) str.push_back(tolower(s[i]));
11         }
12         int n = str.size();
13         for(int i = 0;i < n/2;i ++)
14         {
15             if(str[i] != str[n - 1 - i]) return false;
16         }
17         return true;
18     }
19 };

 

 1 class Solution 
 2 {
 3 public:
 4     bool check(char c)
 5     {
 6         return c>='0' && c<='9' || c>='A' && c<='Z' || c >= 'a' && c <= 'z';
 7     }
 8     bool isPalindrome(string s) 
 9     {
10         for(int i = 0,j = s.size() - 1;i < j;i ++,j --)
11         {
12             while(i < j && !check(s[i])) i++;
13             while(i < j && !check(s[j])) j--;
14             if(s[i] != s[j] && s[i] != (s[j] ^ ' ')) return false;
15         }
16         return true;
17     }
18 };

 

posted @ 2020-04-01 19:48  Jinxiaobo0509  阅读(100)  评论(0)    收藏  举报