valid Palindrome -- 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.
思路:
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相关推荐
- C#LeetCode刷题之#125-验证回文串(Valid Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...
- leetcode Valid Palindrome
题目连接 https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Description Given a string, de ...
- 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...
- leetcode python3 简单题125. Valid Palindrome
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十五题 (1)题目 英文: Given a string, determin ...
- 【回文串14】LeetCode 680. Valid Palindrome II
LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...
- 【回文串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 ...
- [LeetCode] Palindrome Number Valid Palindrome - 回文系列问题
题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如1 ...
最新文章
- C# 获取 IE 临时文件
- NodeJS、NPM安装配置步骤(windows版本)
- 网络营销——网站在网络营销优化中不收录了怎么办呢?
- com组件的ref有时需要有时不需要?_Vue3组件通信总结
- 从性能参数到业务大数据,浅谈直播CDN服务监控
- 搜索推荐中的召回匹配模型综述(一):传统方法
- PHP underlying structure
- 大数据技术在应急事件处理中的启示
- TextAppearance.Material.Widget.Button.Inverse,Widget.Material.Button.Colored
- 史上最强 Java 学习路线图!
- 2021.9.9 prescan文档翻译 Adaptive Cruise Control System
- html自动弹出公告代码,网页html弹窗公告代码
- Golang的chan阻塞测试
- Hadoop集群塔建常见bug
- 一位大学生的自我救赎之路
- Docker服务的重启服务命令(systemctl restart docker)
- 解决rdm连接虚拟机redis失败,idea无法连接
- c语言程序和plc程序的区别,一文告诉你PLC与计算机的本质区别在哪里!小白都能看懂!...
- 一种网络用户行为日志自动获取方法
- 如何更好的选择图像采集卡