zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    Easy题不easy啊,我提交了好几次都没过,各种case都没有考虑清楚就开始做了。两个pointers这个我倒是一上来就确定了。

      1.没有考虑清楚边界,其实两个指针往分两头往中间扫,当他们彼此超越的时候就应该结束循环。因为两个指针相互交错的时候如果还没有检测到不匹配的字符那肯定是回文串无误。

      2.没读懂题目,不造alphanumberic是”数字和字母组成“的意思。。。

    bool isAlphanumeric(char c) {
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
    }
    
    bool isPalindrome(string s) {
        int i = 0 ,j = (int)s.length()-1;
        while (i <= j) {
            if (!isAlphanumeric(s[i])) {
                i++;
                continue;
            }
            if (!isAlphanumeric(s[j])) {
                j--;
                continue;
            }
            if (tolower(s[i]) != tolower(s[j])) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
  • 相关阅读:
    网络流24题之圆桌问题
    BZOJ 4276: [ONTAK2015]Bajtman i Okrągły Robin
    网络流24题航空路线问题
    BZOJ1038 瞭望塔
    BZOJ4029 HEOI2015定价
    BZOJ1226 SDOI2009学校食堂
    网络流24题之魔术球问题
    网络流24题之最小路径覆盖问题
    【BZOJ1098】[POI2007]办公楼biu
    BZOJ3065 带插入区间K小值
  • 原文地址:https://www.cnblogs.com/agentgamer/p/4085045.html
Copyright © 2011-2022 走看看