WHCSRL 技术网

[LeetCode]Valid Palindrome_weixin

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. int len=s.size();
  5. if(len==0||len==1) return true;
  6. int i=0,j=len-1;
  7. while(i<=j)
  8. {
  9. if((s[i]<'a'||s[i]>'z')&&(s[i]<'A'||s[i]>'Z')&&(s[i]<'0'||s[i]>'9')) i++;
  10. else if((s[j]<'a'||s[j]>'z')&&(s[j]<'A'||s[j]>'Z')&&(s[j]<'0'||s[j]>'9')) j--;
  11. else if(s[i]!=s[j]&&(s[i]-s[j]!='A'-'a')&&(s[i]-s[j]!='a'-'A')) return false;
  12. else
  13. {
  14. i++;
  15. j--;
  16. }
  17. }
  18. return true;
  19. }
  20. };

  

转载于:https://www.cnblogs.com/Rosanna/p/3463578.html

文章知识点与官方知识档案匹配,可进一步学习相关知识
推荐阅读