zoukankan      html  css  js  c++  java
  • 验证回文字符串---简单

    题目:

      给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

    示例:

    示例 1:

    输入: "A man, a plan, a canal: Panama"
    输出: true
    

    示例 2:

    输入: "race a car"
    输出: false

    思路:

      先把字符串转小写,然后使用双指针。、

    bool is_alpha(char c)
    {
        if(c>='A'&&c<='Z') return true;
        if(c>='a'&&c<='z') return true;
        if(c>='0'&&c<='9') return true;
        return false;
    }
    class Solution {
    public:
        bool isPalindrome(string s) {
            transform(s.begin(),s.end(),s.begin(),::tolower);
            int length=s.size();
            int i=0,j=length-1;
            while(i<j)
            {
                while((!is_alpha(s[i]))&&i<j) i++;
                while((!is_alpha(s[j]))&&i<j) j--;
                if(s[i]!=s[j]&&i<j) return false;
                i++;j--;
            }
            return true;
        }
    };
  • 相关阅读:
    I'm Telling the Truth
    B-shaass and lights codeForces
    1
    不容易系列之(4)——考新郎 HDU
    犯人冲突
    不互质的和
    OI回忆录
    NOI2018退役记
    uoj221【NOI2016】循环之美
    uoj220【NOI2016】网格
  • 原文地址:https://www.cnblogs.com/manch1n/p/10320476.html
Copyright © 2011-2022 走看看