• valid-palindrome leetcode C++


    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.

    C++

    class Solution {
            bool isAlphaNum(char &ch) {
            if (ch >= 'a' && ch <= 'z') return true;
            if (ch >= 'A' && ch <= 'Z') return true;
            if (ch >= '0' && ch <= '9') return true;
            return false;
        }
    public:
        bool isPalindrome(string s) {
            int left = 0, right = s.size() - 1 ;
            while (left < right) {
                if (!isAlphaNum(s[left])) ++left;
                else if (!isAlphaNum(s[right])) --right;
                else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
                else {
                    ++left; --right;
                }
            }
            return true;
        }
        bool isPalindrome2(string s){
            if("" == s) return true;
            int left = 0;
            int right = s.size() - 1;
            while (left<right){
                if(!isAlphaNum(s[left])) left++;
                else if (!isAlphaNum(s[right])) right--;
                else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
                else { left++; right--;}
            }
            return true;
        }
    };
  • 相关阅读:
    c++作用域运算符---7
    REDIS类和方法说明
    netty WEBSOKET 客户端 JAVA
    出入库算法
    演讲的要义
    别人的面试经历
    在线表单生成器
    windows server 2012 安装 VC14(VC2015) 安装失败解决方案
    esxi 配置 交换主机 虚拟机交换机 linux centos 配置双网卡
    Linux下开发常用配置
  • 原文地址:https://www.cnblogs.com/vercont/p/10210242.html
Copyright © 2020-2023  润新知