关闭

Valid Palindrome

438人阅读 评论(0) 收藏 举报
分类:

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.

给出一个字符串,检查它是不是一个回文。判断过程中只考虑字母数字的字符并且忽略大小写。

例如:

"A man, a plan, a canal: Panama"是一个回文

"race a car"不是一个回文

注意:

 我们定义空字符串为回文

//直接利用俩下标操作!

 bool isPalindrome(string s) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        
      int left,right;
      right = s.size()-1;
      left = 0;
      
      while(right > left)
      {
          if(!isalnum(s.at(right)))right --;//移动
          
          else if(!isalnum(s.at(left)))left ++;//移动
          
          else if(tolower(s.at(right))== tolower(s.at(left)))//最小化处理!
          {
              right --;
              left ++;
          }
          else
          {
              return false;
          }
      }
      return true;
      
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82435次
    • 积分:2101
    • 等级:
    • 排名:第18790名
    • 原创:132篇
    • 转载:37篇
    • 译文:0篇
    • 评论:0条
    文章分类