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 };
  • 相关阅读:
    地铁开发进度1
    人月神话阅读笔记二
    学习进度五
    人月神话阅读笔记一
    WebView的用法:
    图框联动
    阅读笔记《人月神话》一
    5
    4
    3
  • 原文地址:https://www.cnblogs.com/zhengjiankang/p/3679634.html
Copyright © 2011-2022 走看看