
[Easy] 125. Valid Palindrome
发布日期:2021-05-07 18:21:23
浏览次数:23
分类:精选文章
本文共 1172 字,大约阅读时间需要 3 分钟。
- Valid Palindrome
Given a string, determine if it is a palindrome by considering only alphanumeric characters and ignoring cases. Note that an empty string is considered a valid palindrome.
Example 1: Input: "A man, a plan, a canal: Panama" Output: true
Example 2: Input: "race a car" Output: false
Solution Code:
class Solution { bool isPalindrome(string s) { if (s.empty()) return true; int j = s.size() - 1; int i = 0; while (i < j) { if (!isalnum(s[j])) { j--; continue; } if (!isalnum(s[i])) { i++; continue; } if (tolower(s[i++]) != tolower(s[j--])) { return false; } } return true; }}
This solution efficiently checks for palindromes by comparing characters from both ends of the string, ignoring non-alphanumeric characters and case differences. The algorithm runs in O(n) time complexity, where n is the length of the string, making it optimal for this problem.
Note: The comparison is done in a case-insensitive manner to ensure uniformity in validation.
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月24日 17时51分19秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
仿小米商城(上)
2019-03-04
【30】kotlin 闭包
2019-03-04
文件md5怎么会变化
2019-03-04
好玩的editText
2019-03-04
自动安装服务2
2019-03-04
js的各种数据类型判断(in、hasOwnProperty)
2019-03-04
严格模式、混杂模式与怪异模式
2019-03-04
一篇文章带你搞定 Java 中字符流的基本操作(Write / Read)
2019-03-04
HTML 和 CSS 简单实现注册页面
2019-03-04
(SpringMVC)springMVC.xml 和 web.xml
2019-03-04
(LeetCode)Java 求解搜索旋转排序数组
2019-03-04
DP - Tickets - HDU - 1260
2019-03-04
Spring 与使用STOMP消息
2019-03-04
Java Swing JList:列表框组件
2019-03-04
jQuery中的动画
2019-03-04
狂神说MySQL01:初识MySQL
2019-03-04
1.2.3 项目、项目集、项目组合以及运营管理之间的关系
2019-03-04
光环和你一起迎接改版
2019-03-04