[Leetcode] 125. Valid Palindrome

SpringBoot项目搭建 阅读详情

Description:
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.
题目很明确说了空字符串也是回文串,要考虑空字符串的情况。

解法一:
基本思想:对撞指针。
思路:解法一很直接,从头从尾部分别同时遍历,遇到非字母数字(Description中的alphanumeric)跳过去,如果都是alphanumeric,同时变为大写,判断是否相同,直到两个指针相遇。同时也要考虑空串的情况。
这个思路我也想到了,但是实现上对字符串的操作不是很熟练。难点在于:
1. 如何对字符串中每个字符进行操作?A: 类似数组,中括号加索引
2. 如何判断是否为空串?A:类似数组和容器,用.size()方法
3. 如何让字符转为大写或小写?A: toupper(x), tolower(x) for personal use要包含头文件<ctype>
4. 如何判断某字符是否alphanumeric? A: isalnum(x)判断x,如果是,返回1,否返回0

class Solution {
public:
    bool isPalindrome(string s) {

        if(s.size()==0)
            return true;

        for(int i = 0, j = s.size()-1; i < j; i++,j--){
            while(!isalnum(s[i]) && i < j) // i遇到非字母且i<j时,i++
                i++;
            while(!isalnum(s[j]) && i < j) // j遇到非字母且i<j时,j--
                j--;
            if(toupper(s[i]) != toupper(s[j]))
                return false;
        }
        return true;
    }

};

解法二:
基本思想:栈。
思路:其实也想到用栈去做,但是这道题用栈可能让解答更麻烦了,有时间可以再实现一些。但是自己的进步就是想到了栈这个解法。

彩蛋:”Live on evasions? No I save no devil.”

本地通知与健康数据的隐私保护方案 随着移动健康(mHealth)应用的普及,本地通知与健康数据的结合为用户提供了更加个性化的健康管理体验。然而,这种便利性也带来了严峻的隐私保护挑战。本文旨在探讨如何在移动应用中实现本地通知功能的同时,确保用户健康数据的隐私安全。本地通知与健康数据结合的技术架构隐私保护的核心技术方案具体算法实现与数学模型实际应用案例分析本文首先介绍背景知识和核心概念,然后深入探讨隐私保护的技术方案,包括算法原理和数学模型。接着提供实际代码实现和应用场景分析,最后讨论未来发展趋势和挑战。 阅读详情

相关推荐

RV1126平台JD9366触摸屏驱动移植实战:从源码到烧录的完整指南

本文详细介绍了在RV1126平台上移植JD9366触摸屏驱动的完整流程,从环境准备、源码获取到驱动配置、内核编译、设备树设置以及固件烧录与测试验证。通过实战经验分享和常见问题排查,帮助开发者高效完成触摸屏驱动移植,适用于嵌入式Linux开发场景。

weixin_30872337的博客 408

LeetCode 125. Valid Palindrome

LeetCode 125. Valid Palindrome Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring other cases. For example, “A man, a plan, a cana

Vimer.Bai的博客 229

手把手教你安装 Vivado2022.2(附安装包)

Vivado 2022.2版本与之前的版本相比,具有以下几个显著的优势:电源设计管理器(PDM):Vivado 2022.2引入了全新的电源设计管理器(PDM),这是一个下一代功耗评估平台,为Versal® 和 Kria™ SOM提供了准确一致的功耗估计能力。PDM是Versal产品家族的首选功耗评估工具,为Versal ACAP硬IP块提供增强向导,简化从XPE向PDM的迁移,帮助用户获得准确的功耗评估。

dpwkj的博客 2万+

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

批处理脚本手动双击可以执行,但计划任务中执行失败

问题现象: 使用批处理脚本从linux端(samba server) 备份一些数据,设置计划任务每天凌晨两点定时备份。 1)、windows系统中映射共享文件夹为S:盘,需要输入共享用户名和密码 2)、脚本中使用xcopy命令复制共享目录中的文件到windows本地目录中。 脚本如下: set Name=%date:~0,4%%date:~5,2%%date:~8,2% chcp 65001 REM chcp防止中文乱码 mkdir "E:\脚本备份\bakscripts%Name%" xco

独孤清扬玩DB 4312

解构LOL数值平衡:从基础公式到实战决策的深层逻辑

本文深入解析了《英雄联盟》数值平衡的底层逻辑,从基础攻击力公式到经济系统设计,揭示了MOBA游戏背后的精密数学架构。通过实战案例展示护甲减伤、技能伤害建模等核心机制,探讨装备定价与战斗节奏的数值关联,帮助玩家理解版本更新中的平衡艺术。

weixin_42540248的博客 347

[leetcode]125. Valid Palindrome

[leetcode]125. Valid Palindrome Analysis 周五啦周五啦,网上买的小南瓜到啦—— [六一快乐,嘻嘻~] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: Fo...

weixin_32135877的博客 130

基于深度学习的推荐系统设计与实现

基于深度学习的推荐系统设计与实现 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming / TextGenWebUILLM 基于深度学习的推荐系统设计与实现 关键词:深度学习, 推荐系统, 用

AI天才研究院 1078

LeetCode: 125. Valid Palindrome

LeetCode: 125. Valid Palindrome 题目描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example: &quot;A man, a plan, a canal: Panama&quot; is ...

杨领well的专栏 699

2026华为OD机试(JavaScript)真题【E卷+A卷+B卷+C卷+D卷】目录+在线OJ

华为OD机试真题题库【E卷+A卷+B卷+C卷+D卷】目录,JavaScript实现。

MISAYAONE的博客 8万+

字节最新开源模型 DA3(Depth Anything 3)使用教程(一)从任意视角恢复视觉空间

本文介绍DepthAnything3(DA3)模型,一种能够从任意视觉输入中预测几何结构的创新解决方案。DA3采用Transformer骨干网络,通过统一的深度光线表示简化学习过程,支持单目/多视角深度估计、姿态估计和3D高斯预测等多项任务。模型系列包含基础版、度量级和单目专用版本,其中嵌套版本DA3NESTED-GIANT-LARGE表现最佳。代码库提供交互界面、CLI工具和多格式导出功能,支持模块化扩展。实验表明DA3在各项任务上超越现有模型,所有训练数据均来自公开学术数据集。

2506_90492529的博客 2548

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 palindro...

LeeWei 329

微信小程序报错:“errno“:600001,“errMsg“:“uploadFile:fail errcode:-105 cronet_error_code:-105 error_msg:net::

uploadFile:fail errcode:-105 cronet_error_code:-105 error_msg:net::ERR_NAME_NOT_RESOLVED,uniApp微信小程序使用uni.uploadFile出现报错,微信小程序如何解决wx.uploadFile报错,如何解决这个问题,微信小程序真机报错,微信小程序报错"errno":600001,"errMsg":"uploadFile:fail errcode:-105 cronet_error_code:-105 error_m

✅ 精选推荐文章 3826

leetcode 125. Valid Palindrome

leetcode 125. Valid Palindrome 题目大意:判断一句话时候为回文,只考虑字母,忽略大小写。 解题思路: 1、判断一个字符是否为数字或者字母 2、在比较的时候不分大小写,因此大写直接全部转为小写 3、在判断回文时直接比较首尾的两个字符

编码之路 299

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode125—Valid Palindrome

LeetCode125—Valid Palindrome

上善若水 806

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【LeetCode125】Valid Palindrome

Runtime:8 ms, faster than85.46%ofC++online submissions forValid Palindrome. Memory Usage:9.4 MB, less than61.22%ofC++online submissions forValid Palindrome. class Solution { public: ...

weixin_39458342的博客 153

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

【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

【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 ca

斗code、年华 540

LeetCode 125 Valid Palindrome

LeetCode 125 Valid Palindrome Problem Description: 判断给出的字符串是否是有效的回文字符串。 具体的题目信息: https://leetcode.com/problems/valid-palindrome/description/ Solution: 解题思路:用两个变量i和j从字符串两端向中间移动,比较两个变量指向的字符 (1)如...

萤火虫啊飞呀飞 279

【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

【回文串3】LeetCode 125. Valid Palindrome

LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊。 class Solution { public: bool isPalindrome(string s) { int n = s.size(); if (n &amp;lt;= 1) return true;...

Allenlzcoder的博客 282

【LeetCode】125. Valid Palindrome

LeetCode125 传送门 解法比较简单: 两个指针,分别从左向右,从右向左遍历字符串,时间复杂度,空间复杂度。 Python源码: class Solution: def isPalindrome(self, s: str) -> bool: s = s.lower() left_p = 0 right_p = ...

weixin_42344158的博客 138

LeetCode 125. Valid Palindrome 题解

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

cannonjinx的博客 4175
上一篇: 排序算法大总结
下一篇: [Leetcode] 344. Reverse String
Peggy_Chang
博客等级 码龄11年 21粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值