首页 > 代码库 > Valid Palindrome

Valid Palindrome

 

 1 class Solution { 2 public: 3     bool isPalindrome(string s) { 4         int strLen = s.size(); 5         int pos_h = 0; 6         int pos_t = 0; 7         char *copy = new char[strLen]; 8         int num = 0; 9         for(int i=0;i<strLen;i++)10         {11             if(s[i] >= 65 && s[i]  <= 90 || s[i] >= 48 && s[i] <= 57)12               {13                   *(copy+num) = s[i];14                   num++;15               }16               else if(s[i] >= 97 && s[i]  <= 122)17               {18                   *(copy+num) = s[i] - 32;19                   num++;20               }21         }22        if(num == 0 || num == 1)23             return true;24         pos_h = 0;25         pos_t = num - 1;26         for(int i=0;i<num/2;i++)27             {28                if(copy[pos_h] != copy[pos_t])29                     break;30                pos_h++;;31                pos_t--;32             }33             if(pos_h < pos_t )34                 return false;35             else36                 return true;37     }38 };

 

Valid Palindrome