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;
    }
  • 相关阅读:
    SDWebImage内部实现过程
    物理仿真元素
    物理仿真元素
    运行时案例
    使用运行时交换我们自定义的方法
    运行时交换系统方法
    HTML 钟表 小时钟
    JS小游戏寻找房祖名
    程序启动的完整过程
    ApplicationDelegate里的方法
  • 原文地址:https://www.cnblogs.com/agentgamer/p/4085045.html
Copyright © 2011-2022 走看看