题目:
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相关推荐

  1. 【回文串3】LeetCode 125. Valid Palindrome

    LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊. class Solution { public:boo ...

  2. [勇者闯LeetCode] 125. Valid Palindrome

    [勇者闯LeetCode] 125. Valid Palindrome Description Given a string, determine if it is a palindrome, con ...

  3. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  4. 125 Valid Palindrome

    125 Valid Palindrome 链接:https://leetcode.com/problems/valid-palindrome/ 问题描写叙述: Given a string, dete ...

  5. 【回文串14】LeetCode 680. Valid Palindrome II

    LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...

  6. leetcode python3 简单题125. Valid Palindrome

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十五题 (1)题目 英文: Given a string, determin ...

  7. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  8. LeetCode OJ - Valid Palindrome

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  9. 125. Valid Palindrome

    Title 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a pla ...

最新文章

  1. 如何读取服务器的文件夹大小,请教如何获取outlook文件夹对话框中服务器数据大小?...
  2. 女鬼之阴魂不散(C#版)
  3. 自己理解的比特币双重支付
  4. 二、kafka基本操作和管理
  5. 用crontab、crond在嵌入式系统中添加定时任务
  6. java中aliases的意思,Java Charset.aliases方法代码示例
  7. C#编译器对泛型方法调用作类型推断的奇怪问题
  8. 笔记:复杂度分析(上)
  9. 五个转义气符html,【转】前端开发攻城师绝对不可忽视的五个HTML5新特性
  10. 小米6无线显示无法连接服务器,小米手机:无法连接WiFi怎么解决?
  11. ZZULIOJ 1190: 按出生日期排序(结构体专题)
  12. python木马病毒_Python引导木马病毒(拓展篇)
  13. nginx url中带中文不能访问
  14. 【日常积累】hr标签的属性及样式
  15. 让remix使用本地文件系统
  16. 固态硬盘比机器硬盘好很多吗?
  17. 阿里云人脸对比API使用
  18. python爬取网易云热歌榜
  19. 《ucore lab1 练习5》实验报告
  20. Fcoin平台每天赠送了好多币如何处理呢?

热门文章

  1. windows mobile 服务自动停止_打印服务print Splooer自动停止怎么办?
  2. 302状态码_HTTP状态码status code详解
  3. c语言uint8的数组怎么转换为uint32_剖析JS和Redis的数据结构设计:数组
  4. RabbitMQ集群并处理失败
  5. 0.5px边框,css及sass
  6. 迅雷下载的都是download.php,CMS_dedecms 下载地址加迅雷专用链的操作方法 比较全,特别注意!!!!!!!!! - phpStudy...
  7. spring boot mybatisplus集成_spring-boot系列之集成测试
  8. 删除本地缓存localStorage定义的字段 - 代码篇
  9. 小储云v1.782免授权版
  10. 黑白极简设计行业企业官网模板