zoukankan      html  css  js  c++  java
  • 【LeetCode】125. 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.

    提示:

    比较简单的题目,要注意下面三点即可:

    • 忽略大小写
    • 只考虑数字和英文字母
    • 空字符串符合要求

    代码:

    class Solution {
    public:
        bool isPalindrome(string s) {
            if (s.size() == 0) return true;
            for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
                while (!isalnum(s[i]) && i < s.size()) ++i;
                while (!isalnum(s[j]) && j > 0) --j;
                if (tolower(s[i]) != tolower(s[j]) && i < j) return false;
            }
            return true;
        }
    };
  • 相关阅读:
    C# 6.0
    C# 4.0
    C# 5.0
    C# 3.0
    C# 2.0
    C# 1.0(2002)
    字典树Trie
    Hadoop——生态体系
    程序是怎样跑起来的
    Redis实战(十七)Redis各个版本新特性
  • 原文地址:https://www.cnblogs.com/jdneo/p/4750918.html
Copyright © 2011-2022 走看看