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.

思路:
1. 定义两个指针,分布指向字符串的首尾
2. 从头开始,过滤头部的非字母数字字符
3. 然后从尾部开始,过尾部的非字母数字字符
4. 头指针小于尾指针的情况下,
1) 如果两个字母数字字符相同,
2)头指针后移,并过滤非字母数字字符
5. 如果两个字母不同,返回false

代码:

class Solution {
public:bool isPalindrome(string s) {if (s.size() == 0 || s.size() == 1) return true;int low = 0, high = s.size() - 1;//从头开始,过滤头部的非字母数字字符while (low < s.size() && !isRightChar(s[low])) ++low;//从尾开始,过滤尾部的非字母数字字符while (high >= 0 && !isRightChar(s[high])) --high;while(low < high){if (lowerCase(s[low]) == lowerCase(s[high])){//头指针后移,并过滤非数字字母字符++low;while (low < s.size() && !isRightChar(s[low])) ++low;//尾指针前移,并过滤非数字字母字符--high;while (high >= 0 && !isRightChar(s[high])) --high;}else return false;}return true;}//判读是否为数字字母字符bool isRightChar(char c){if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) return true;else return false;}//大写转小写char lowerCase(char c){if (c >= 'A' && c <= 'Z') return tolower(c);return c;}
};

转载于:https://www.cnblogs.com/xiaocai-ios/p/7779765.html

valid Palindrome -- leetcode相关推荐

  1. C#LeetCode刷题之#125-验证回文串(Valid Palindrome)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...

  2. leetcode Valid Palindrome

    题目连接 https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Description Given a string, de ...

  3. 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...

  4. leetcode python3 简单题125. Valid Palindrome

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

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

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

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

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

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

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

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

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

  9. [LeetCode] Palindrome Number Valid Palindrome - 回文系列问题

    题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如1 ...

最新文章

  1. C# 获取 IE 临时文件
  2. NodeJS、NPM安装配置步骤(windows版本)
  3. 网络营销——网站在网络营销优化中不收录了怎么办呢?
  4. com组件的ref有时需要有时不需要?_Vue3组件通信总结
  5. 从性能参数到业务大数据,浅谈直播CDN服务监控
  6. 搜索推荐中的召回匹配模型综述(一):传统方法
  7. PHP underlying structure
  8. 大数据技术在应急事件处理中的启示
  9. TextAppearance.Material.Widget.Button.Inverse,Widget.Material.Button.Colored
  10. 史上最强 Java 学习路线图!
  11. 2021.9.9 prescan文档翻译 Adaptive Cruise Control System
  12. html自动弹出公告代码,网页html弹窗公告代码
  13. Golang的chan阻塞测试
  14. Hadoop集群塔建常见bug
  15. 一位大学生的自我救赎之路
  16. Docker服务的重启服务命令(systemctl restart docker)
  17. 解决rdm连接虚拟机redis失败,idea无法连接
  18. c语言程序和plc程序的区别,一文告诉你PLC与计算机的本质区别在哪里!小白都能看懂!...
  19. 一种网络用户行为日志自动获取方法
  20. 如何更好的选择图像采集卡

热门文章

  1. 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案
  2. Linux添加文件命令
  3. 计算机网络与网页制作教程,计算机网络与网页制作:Dreamweaver CS5案例教程/高等学校通识教育系列教材简介,目录书摘...
  4. 干货|对某杀猪盘的渗透测试
  5. 随机森林原理详解及python代码实现
  6. 未来数据领域的珠穆朗玛峰之中文自然语言处理
  7. Scala入门到精通——第二十七节 Scala操纵XML
  8. 携程基于Quasar协程的NIO实践
  9. 为什么说 GraphQL 可以取代 REST API?
  10. nginx学习九 upstream 负载均衡