Valid Palindrome

来源:互联网 发布:塔吉克斯坦 知乎 编辑:程序博客网 时间:2024/04/25 23:48

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;          }