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;
    }
  • 相关阅读:
    pwn1_sctf_2016
    warmup_csaw_2016
    网鼎杯2020 joker逆向
    网鼎杯2020 伪虚拟机wp
    WannaRen病毒逆向分析
    v2ex源代码相关资料
    iOS自学
    ios牛博
    你有什么问题需要问我的吗?
    类族的写法
  • 原文地址:https://www.cnblogs.com/agentgamer/p/4085045.html
Copyright © 2011-2022 走看看