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 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.
思路:
判断一个字符串(数字字母)是否是回文字符串,不计空格和标点。
如果s的长度小于等于1,直接返回true,设置两个索引,即双指针,当前一个索引比后一个索引小时,如果这两个都是字母或者数字,如果这两个相等(不计大小写),i往后,j往前,继续下一个循环;否则直接返回false。如果i位置不为字母或者数字,i后移; 如果j位置不为字母或者数字,j前移。如果j位置不为字母或者数字,j前移。
代码:
class Solution {
public:bool isPalindrome(string s) {int len=s.length();if(len<=1){//如果s的长度小于等于1,直接返回truereturn true;}int i=0;//设置两个索引,即双指针 int j=len-1;while(i<j){//当前一个索引比后一个索引小时if(isalnum(s[i])&&isalnum(s[j])){//如果这两个都是字母或者数字if(toupper(s[i])==toupper(s[j])){//如果这两个相等(不计大小写)++i;//i往后,j往前,继续下一个循环--j;continue;}else{//否则直接返回falsereturn false;}}else if(!isalnum(s[i])){//如果i位置不为字母或者数字,i后移++i;}else if(!isalnum(s[j])){//如果j位置不为字母或者数字,j前移--j;}}return true;//循环结束,如果没有返回,则一定是true}
};
**输出结果:**9ms
LeetCode 125. Valid Palindrome相关推荐
- 【回文串3】LeetCode 125. Valid Palindrome
LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊. class Solution { public:boo ...
- [勇者闯LeetCode] 125. Valid Palindrome
[勇者闯LeetCode] 125. Valid Palindrome Description Given a string, determine if it is a palindrome, con ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...
- 125 Valid Palindrome
125 Valid Palindrome 链接:https://leetcode.com/problems/valid-palindrome/ 问题描写叙述: Given a string, dete ...
- 【回文串14】LeetCode 680. Valid Palindrome II
LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...
- leetcode python3 简单题125. Valid Palindrome
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十五题 (1)题目 英文: Given a string, determin ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- LeetCode OJ - Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 125. Valid Palindrome
Title 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a pla ...
最新文章
- 如何读取服务器的文件夹大小,请教如何获取outlook文件夹对话框中服务器数据大小?...
- 女鬼之阴魂不散(C#版)
- 自己理解的比特币双重支付
- 二、kafka基本操作和管理
- 用crontab、crond在嵌入式系统中添加定时任务
- java中aliases的意思,Java Charset.aliases方法代码示例
- C#编译器对泛型方法调用作类型推断的奇怪问题
- 笔记:复杂度分析(上)
- 五个转义气符html,【转】前端开发攻城师绝对不可忽视的五个HTML5新特性
- 小米6无线显示无法连接服务器,小米手机:无法连接WiFi怎么解决?
- ZZULIOJ 1190: 按出生日期排序(结构体专题)
- python木马病毒_Python引导木马病毒(拓展篇)
- nginx url中带中文不能访问
- 【日常积累】hr标签的属性及样式
- 让remix使用本地文件系统
- 固态硬盘比机器硬盘好很多吗?
- 阿里云人脸对比API使用
- python爬取网易云热歌榜
- 《ucore lab1 练习5》实验报告
- Fcoin平台每天赠送了好多币如何处理呢?
热门文章
- windows mobile 服务自动停止_打印服务print Splooer自动停止怎么办?
- 302状态码_HTTP状态码status code详解
- c语言uint8的数组怎么转换为uint32_剖析JS和Redis的数据结构设计:数组
- RabbitMQ集群并处理失败
- 0.5px边框,css及sass
- 迅雷下载的都是download.php,CMS_dedecms 下载地址加迅雷专用链的操作方法 比较全,特别注意!!!!!!!!! - phpStudy...
- spring boot mybatisplus集成_spring-boot系列之集成测试
- 删除本地缓存localStorage定义的字段 - 代码篇
- 小储云v1.782免授权版
- 黑白极简设计行业企业官网模板