zoukankan      html  css  js  c++  java
  • 125. 验证回文串

    题目:给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。

    示例 1:

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

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

    1.原创

    class Solution {
    public:
        bool isPalindrome(string s) {
            string res1;
            for (int i=0;i<s.length();++i){
                if (isalnum(s[i]))//判断字符是否为字母
                    res1+=tolower(s[i]);
            }
            if (res1.length()<2)
                return true;
            string res2 = res1;
            reverse(res1.begin(),res1.end());
            ////以上两句可以改为:string res2(res1.rbegin(), res1.rend());
            if (res1==res2)
                return true;
            else
                return false;
        }
    };

    2.题解

    class Solution {
    public:
        bool isPalindrome(string s) {
            string sgood;
            for (char ch: s) {
                if (isalnum(ch)) {
                    sgood += tolower(ch);
                }
            }
            int n = sgood.size();
            int left = 0, right = n - 1;
            while (left < right) {
               if (sgood[left] != sgood[right]) {
                    return false;
                }
                ++left;
                --right;
            }
            return true;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/valid-palindrome/solution/yan-zheng-hui-wen-chuan-by-leetcode-solution/
  • 相关阅读:
    Mysql集群
    Redis集群
    Python3 实现数据读写分离设计
    PHP Session的优化使用
    防盗链与token运用
    PHP与REDIS
    优化设计提高sql类数据库的性能
    Nodejs密集型CPU解决方案
    可重入和线程安全
    信号处理函数编写规则
  • 原文地址:https://www.cnblogs.com/USTC-ZCC/p/14505210.html
Copyright © 2011-2022 走看看