[Leetcode] 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.
题意:给定字符串,判断是否为回文。值得注意的是,只考虑字符和数字且不考虑字符大写小,其他情况忽略。
思路:判断是否为回文的情况,一般都是用两个指针,从字符串的两端开始向中间移动,若对应位置的字符不等则返回false。这里的区别在于,有空格和符号,而这些都是题中要求忽略的,所以,每当遇到空格和符号时,直接跳过就行,反应在代码上就是一个加加,一个减减;还有一点就是,这里是不考虑大小写的,所以需要写一个函数,遇到大写的时候转化成小写去比较(也可以小转大),其他不变。代码如下:
class Solution {
public:
bool isPalindrome(string s)
{
int len=s.size();
if(len==) return true;
int l=,r=len-;
while(l<r)
{
if( !isAlpnum(s[l]))
l++;
else if( !isAlpnum(s[r]))
r--;
else if(toLower(s[l]) !=toLower(s[r]))
return false;
else
{
l++;
r--;
}
}
return true;
}
bool isAlpnum(const char c)
{
if('A'<=c&&c<='Z')
return true;
else if('a'<=c&&c<='z')
return true;
else if(''<=c&&c<='')
return true;
return false;
}
char toLower(const char c)
{
if('A'<=c&&c<='Z')
return c+;
return c;
}
};
其中判断是否为字母和数字可以用函数isalnum,这样就可以少写一个函数,其余不变。参考了booirror的博客。
[Leetcode] valid palindrome 验证回文的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
随机推荐
- react-native android 初始化问题
最近开始接触rn,官方起手,装了一堆工具,然后启动项目的时候出现了一堆问题,这里针对我遇到的一些问题提供一些解决方案. 本人开发环境mac,在启动ios的时候没啥大问题,可以直接启动,这里提示一点,因 ...
- JMeter 录制APP脚本
具体步骤: 1.启动JMeter: 2.在“测试计划”中添加“线程组”: 3.“工作台”中添加“HTTP代理服务器”: 4.配置代理服务器:端口:8888(默认值),HTTPS Domains:192 ...
- H5应用程序缓存浅谈及实际测试
应用程序缓存能做什么? 可以在脱离网络的条件下离线访问. 减少读取服务器文件,减轻服务器的访问压力. 优化网站打开速度. 如何启用应用缓存? 第一步:给服务器添加新的MIME:扩展名:.appcach ...
- Simple layout
body { padding: 0; margin: 0; overflow: hidden; } div { display: block; position: relative; } .c ...
- 150命令之线上查询及帮助命令 man hellp
150命令之线上查询及帮助命令 man 查询命令的帮助 man + 命令 NAME ls - list directory contents 命令+命令简单说明 SYNOPSIS ...
- 5.安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- Pipeline组Beta版本发布说明
项目名称 Pipeline 项目版本 Beta版本 负责人 北京航空航天大学计算机学院 IloveSE 小组 联系方式 http://www.cnblogs.com/IloveSE 要求发布日期 20 ...
- spring框架(1)— 依赖注入
依赖注入 spring核心容器就是一个超级大工厂,所以的对象(数据源.hibernate SessionFactory等基础性资源)都会被当做spring核心容器的管理对象——spring把容器中的一 ...
- Windows平台下安装.net coreclr
.net coreclr 已经发布RC1版本,安装方法如下: 1.安装DNVM,DNVM是.net运行时管理器,负责管理所有版本的.net运行时(.net framework..net coreclr ...
- linux下安装多个jdk版本的切换问题
下载地址: https://www.azul.com/downloads/zulu/ 解压: [root@localhost java]# tar -zxvf /usr/java/zulu8.38.0 ...