zoukankan      html  css  js  c++  java
  • Valid Palindrome

    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.

    Solution: traverse from both side.

     1 #include <cctype> // needed for isalnum(), isupper() and tolower().
     2 class Solution {
     3 public:
     4     bool isPalindrome(string s) {
     5         for(int i = 0, j = s.size()-1; i < j; i++, j--) {
     6             while(i < j && !isalnum(s[i])) i++;
     7             while(i < j && !isalnum(s[j])) j--;
     8             if(tolower(s[i]) != tolower(s[j])) return false;
     9         }
    10         return true;
    11     }
    12 };
  • 相关阅读:
    学习日报
    阅读笔记2
    学习日报
    记账本开发7
    记账本开发6
    学习日报
    记账本开发5
    今日总结
    今日总结
    家庭记账本7
  • 原文地址:https://www.cnblogs.com/zhengjiankang/p/3679634.html
Copyright © 2011-2022 走看看