leetcode 125. Valid Palindrome 题解

125. Valid Palindrome [easy] (Python) 125. Valid Palindrome [easy] (Python)题目链接https://leetcode.com/problems/valid-palindrome/题目原文 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cas 阅读详情

125. 验证回文串
125. Valid Palindrome

题目:

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

示例 1:

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

示例 2:

输入: “race a car”
输出: false

题解:

主要是先转换为小写,再利用正则表达式删除不是数字和字母的字符。

C++
#include<cctype> //tolwer;
#include<algorithm> // transform;

class Solution {
public:
    bool isPalindrome(string s) {
        transform(s.begin(), s.end(), s.begin(), ::tolower); // 转换为小写
        s = regex_replace(s, regex("[^a-z0-9]"), "");// 去除不是字母和数字的字符
        int i = 0;
        int j = s.size() - 1;
        while(i < j && s[i] == s[j]){
            i++;
            j--; 
        }
        return i >= j;
    } 
};
Java
class Solution {
    public boolean isPalindrome(String s) {
        s = s.toLowerCase();// 转换为小写
        s = s.replaceAll("[^0-9a-z]", "");// 去除不是字母和数字的字符
        int i  = 0;
        int j = s.length() - 1;
        while(i < j) {
            if(s.charAt(i) == s.charAt(j)) {
                i++;
                j--;
            }
            else {
                return false;
            }
        }
        return true;
    }
}
Python
class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower()
        s = re.sub('[^0-9a-z]', '', s) # 去除不是字母和数字的字符
        i = 0
        j = len(s) - 1
        while i < j:
            if (s[i] == s[j]):
                i += 1
                j -= 1
            else:
                return False
        return True
JavaScript
/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
    var reg = /[^0-9a-z]/g   // g表示全局替换,否则只替换第一个
    s = s.toLowerCase()
    s = s.replace(reg, "") // 去除不是字母和数字的字符
    var i = 0
    var j = s.length - 1
    while (i < j) {
        if(s[i] === s[j]) {
            i++;
            j--;
        }
        else {
            return false;
        }
    }
    return true;
};
手把手教你学simulink(8.3)--实例:基于 Simulink 的自动驾驶汽车速度控制系统设计与仿真 基于 Simulink 的自动驾驶汽车速度控制系统设计与仿真。 阅读详情

相关推荐

Multisim 14.0元件库下载权威指南:官方与社区资源

详解Multisim元件库下载的官方途径与可靠社区资源,帮助用户快速获取Multisim 14.0所需元件模型,提升电路仿真效率。

weixin_32925455的博客 747

Palindrome题解

题目链接 题目大意:给一个字符串,求这个字符串的最大回文串的长度 题目分析:回文串问题是一个经典问题,回文串分为奇数长度的回文串和偶数长度回文串两类,且回文串具有一个中心。回文串判定方法:以奇数为例,假设当前回文串的中心为pos,则长度为2 * len + 1的字符串为回文字符串的充要条件为s[pos - len -> pos - 1] == (s[pos + 1 -> pos + l...

gyr679的博客 277

从硬件到软件:深度解析D435i USB SCP overflow错误的根源与解决方案

本文深入解析Intel RealSense D435i相机在ROS环境下常见的'USB SCP overflow'错误,特别是与`control_transfer`相关的错误提示。从硬件配置到软件优化,详细介绍了如何通过调整USB接口、优化`rs_camera.launch`参数、更新固件驱动等方法解决数据传输溢出问题,提升设备稳定性和性能。

yy01234的博客 771

POJ 1159 Palindrome 题解

本题的题意理解之后,就是求最长回文子序列 longest palindrome subsequence,这里注意子序列和子串的区别。 有两种求法,一种是直接求,相当于填矩阵右上对角阵,另一种是转化为longest common subsequence的求法。 最大难点就是要求内存不能使用二维的。 故此第一种方法是有点难度的,因为需要把二维矩阵的对角线转化为一维表记录,对好下标就好了。 第二中

靖空间 1221

PT静态时序分析实战:从基础命令到高级报告解析

本文详细介绍了PT(PrimeTime)静态时序分析(STA)的实战技巧,从基础命令如pt_shell和report_timing的使用,到高级报告解析与问题排查。通过丰富的实例和脚本示例,帮助工程师快速掌握时序分析的核心技能,提升芯片设计验证效率。

weixin_29289821的博客 954

Multisim14安装失败?手把手教你彻底清理残留文件(附专用卸载工具)

本文针对Multisim14安装失败问题,提供了一套从根源解决的完整方案。文章详细分析了安装失败的三大元凶——文件残留、注册表残留和隐藏组件,并手把手指导用户使用专用卸载工具进行标准化清理,再结合手动深度清理文件系统和注册表,最终实现彻底卸载和完美重装,有效解决Multisim14安装问题。

weixin_29179311的博客 317

LeetCode 125. Valid Palindrome 题解

本题是字符串处理的经典问题。双指针技巧字符类型判断大小写处理边界条件处理通过本题可以深入理解双指针在字符串处理中的应用。

cannonjinx的博客 4175

SAP VF01开票报错:出口贸易数据不完整

找了半天资料,最后发现是没有维护凭证抬头的数据;由于我不是SD模块顾问,整体流程使用的是原本环境里面的数据,用新建工厂引用,所以也不知道为啥会出发这个贸易业务。正常传到FICO模块,生成会计凭证、获利分析、利润中心及CO凭证。需要维护这三个字段后才能正常转到FI生成会计凭证。VF01开票报错:出口贸易数据不完整。

MaiKouBei的博客 1540

LeetCode力扣 125. 验证回文串 Valid Palindrome 题解代码 JavaScript

问题https://leetcode-cn.com/problems/valid-palindrome/ 练习使用JavaScript解答 /** * @param {string} s * @return {boolean} */ function judgeChar(c) { if(c >= '0' && c <= '9') return true; if(c >= 'a' && c <= 'z') .

Coding home - 漂流瓶jz 466

PyTorch实战(26)——PyTorch分布式训练

在本节中,我们将探讨如何通过跨机器和机器内多进程的分布式训练来加速模型训练过程。我们将系统学习 PyTorch 提供的三大分布式训练 API——torch.distributed、torch.multiprocessing 以及 torch.utils.data.distributed.DistributedSampler,使用这些 API 能够极大的简化分布式训练,介绍如何使用 PyTorch 的分布式训练工具,在 CPU 和 GPU 上加速训练。

盼小辉丶的博客 3815

AutoSAR配置与实践(深入篇)5.4 OS原理(下 )- 内存保护(MPU)

ECU可能由内部模块、第三方模块等各模块集成的。需求对每个模块所定义的安全等级可能有差异,需要考虑不同安全等级的模块和代码间减少相互干扰,防止程序错误传播。可以理解为包含Task、Alarm、Counter、Schedule table、ISRs、Event等基本对象的容器,用于隔离Task/Alarm等,进行内存保护。

nihaoljq2010的博客 3536

leetcode题解-125. Valid Palindrome

题目:给定一个字符串,判断是不是回文,只考虑字母数字字符,不考虑大小写。另外,空串被认为是有效的回文。分析:保留左右两个指针,每一轮先移动左指针获取一个字母或数字,其它字符都跳过;然后移动右指针,找到一个字母或数字,其它字符都跳过;如果此时左右两个字符都为字母或数字并但是两个不相等,则返回false;进行下轮指针移动直到两个指针相等。时间复杂度:O(n) 空间复杂度:O(1)class Solut

Irving_zhang的专栏 804

肖特基二极管选型参数,结构原理,工艺与注意问题总结

肖特基二极管(Schottky Diode)是一种特殊的二极管,也被称为肖特基势垒二极管。它是利用金属与半导体接触形成的金属-半导体结原理制成的。与传统的PN结二极管相比,肖特基二极管具有更低的正向压降、更高的开关速度、更高的反向恢复时间以及更好的温度稳定性等特点。本文对肖特基二极管的工作原理,结构特点,工艺流程,选型参数及设计注意事项进行总结。

fydar的博客 812

Leetcode题解 125. 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 palin

Xd_Yu的专栏 265

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

leetcode题解-125. Valid Palindrome && 680. Valid Palindrome II

这是两道判断字符串是否为回文的题目,先来看第一道: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. "rac

呜呜哈的博客 910

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode 125. Valid Palindrome

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-125-valid-palindrome/ 解答1[Java]: class Solution { private static final char[] charMap = new char[256]; static { for (int i = 0; i <...

user2639的博客 135

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode125—Valid Palindrome

LeetCode125—Valid Palindrome

上善若水 806

LeetCode刷题:125. Valid Palindrome

LeetCode刷题:125. Valid Palindrome 原题链接:https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. N...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 186

【leetCode】leetCode125 Valid Palindrome

题目如下: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. ...

随手积累,典型bug解决。技术栈的系统介绍。 196

125. Valid Palindrome*

125. Valid Palindrome* https://leetcode.com/problems/valid-palindrome/ 题目描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the ...

珍妮的选择的博客 239

LeetCode--125. Valid Palindrome

题目链接:https://leetcode.com/problems/valid-palindrome/ 这是一道很简单平常的问题,但是非常的恶心,提交了6次才AC,主要是对alphanumeric characters的理解,它的定义参考https://whatis.techtarget.com/definition/alphanumeric-alphameric。知晓了这个定义就十分轻松了,...

To_be_thinking的博客 154

【leetcode】125. Valid Palindrome

125.Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note:For the purpose of this problem, we define empty string as v...

lemonade13的博客 125
上一篇: leetcode 833. Find And Replace in String 题解
下一篇: 《统计学习方法》第二章-感知机-总结
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值