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.

主要考察, cctype.h 的使用:

可能用到的函数: tolower, toupper, isalpha, isdigit, isalnum。

 class Solution {
public:
bool isPalindrome(string s) {
int left=,right =s.size()-;
while(left<right)
{
if(!isalnum(s[left]))
left++;
else if(!isalnum(s[right]))
right--;
else
{
if(tolower(s[left])==tolower(s[right]))
{
left++;
right--;
}
else
return false;
}
}
return true;
}
};

可能用到的函数: tolower, toupper, isalpha, isdigit, isalnum。

补充知识:  cctype 库文件(参考cplusplus:http://www.cplusplus.com/reference/cctype/ )

函数名 function description 函数说明
isalnum Check if character is alphanumeric (function ) 查看参数是否为字符或数字
isalpha Check if character is alphabetic (function ) 查看参数是否为字符
isblank  Check if character is blank (function ) 查看参数是否为空格
iscntrl Check if character is a control character (function ) 查看参数是否为控制字符
isdigit Check if character is decimal digit (function ) 查看参数是否为十进制数字
isgraph Check if character has graphical representation (function ) 查看参数是否为可显示字符
islower Check if character is lowercase letter (function ) 查看参数是否为可打印字符
isprint Check if character is printable (function ) 查看参数是否为小写字符
ispunct Check if character is a punctuation character (function ) 查看参数是否为标点符号
isspace Check if character is a white-space (function ) 查看参数是否为空字符
isupper Check if character is uppercase letter (function ) 查看参数是否为大写字符
isxdigit Check if character is hexadecimal digit (function ) 查看参数是否为十六进制数字

具体的ASCII 与 cctype各函数的对应关系如下表: 

ASCII values characters iscntrl isblank isspace isupper islower isalpha isdigit isxdigit isalnum ispunct isgraph isprint
x00 .. 0x08 NUL, (other control codes) x                      
0x09 tab ('\t') x x x                  
0x0A .. 0x0D (white-space control codes:'\f','\v','\n','\r') x   x                  
0x0E .. 0x1F (other control codes) x                      
0x20 space (' ')   x x                 x
0x21 .. 0x2F !"#$%&'()*+,-./                   x x x
0x30 .. 0x39 123456789             x x x   x x
0x3a .. 0x40 :;<=>?@                   x x x
0x41 .. 0x46 ABCDEF       x   x   x x   x x
0x47 .. 0x5A GHIJKLMNOPQRSTUVWXYZ       x   x     x   x x
0x5B .. 0x60 [\]^_`                   x x x
0x61 .. 0x66 abcdef         x x   x x   x x
0x67 .. 0x7A ghijklmnopqrstuvwxyz         x x     x   x x
0x7B .. 0x7E {|}~                   x x x
0x7F (DEL) x                      

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Valid Palindrome的更多相关文章

  1. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  2. LeetCode(125)题解--Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...

  3. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. LeetCode 1216. Valid Palindrome III

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...

  5. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  7. [leetcode] 1. Valid Palindrome

    leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...

  8. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【leetcode】Valid Palindrome

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

随机推荐

  1. Tomcat实践

    1.1Java环境介绍 jdk  java 开发工具包 jre sdk J2EE 企业版 J2SE 标准版 J2ME 手机开发 1.2Tomcat自动部署 通过saltstack来批量安装tomcat ...

  2. python的disutils创建分发包

    python中的distutils包主要用创建共享包,安装包,在平时安装python模块的时候,使用的命令如下: python setup.py install 其实以上代码就是distuitls包提 ...

  3. Embarcadero RAD Studio 2016 Product Approach and Roadmap

    delphi  2016 路线图 http://community.embarcadero.com/article/news/16211-embarcadero-rad-studio-2016-pro ...

  4. 【冷门】 C# 小技巧之获取变量名称

    今天在自我规范程序设计的时候,变量名匹配字符串来自配置文件,网上找了一会儿发现也有朋友在找寻这种方式,很不容易找到一个解决方案来自http://www.th7.cn/Program/net/20140 ...

  5. 前端开发之HTML篇二

    主要内容: 一.表格标签 -- table 二.表单标签 -- form 三.常用标签属性和分类 四.标签嵌套规则 1️⃣  表格标签 -- table 表格由<table> 标签来定义. ...

  6. sqlserver for xml

    FOR XML子句有四种最基本的模式 1.AUTO模式:返回数据表为起表名的元素,每一列的值返回为属性:2.RAW模式:返回数据行为元素,每一列的值作为元素的属性: 3.PATH模式:通过简单的XPa ...

  7. 二维码生成插件qrious及网站扫码登录的一些理解

    什么是二维码 ​ 二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型. ...

  8. Android中的网络编程

    谷歌在Android6.0之后就废弃了使用HttpClinet进行网络连接.所以,这里需要重点学习的是通过HttpUrlConnect进行网络连接. String path="这里是你想要的 ...

  9. c# ftp 判断目录是否存在和创建文件夹

    工作中项目一直使用的ftp上传日志文件出现了问题,新的服务器搭建好后,日志无法上传.正好来学习一下ftp. 程序中的流程是,一个计时器,每分钟检测配置文件中本地日志文件路径下有没有日志文件,如果有就上 ...

  10. Mask_rcnn openpose realsense

    cd /home/luo/Desktop/MyFile/Mask_RCNN_Openpose_Realsense python realsense_mask_openpose_2019032601.p ...