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. CSS 父级方法清除浮动方法

    .baseMod:after { clear: both; content: ' '; display: block; visibility: none; height: 1% } 2. overfl ...

  2. Netty简单的重连机制

    其实重连机制并不是多么多高深的技术,其实就是一个在客户端做一个简单的判断,如果连接断了,那么就重新调用连接服务端的代码 当然,我们重连的动作肯定是发生在断连之后发生的,我们可以在上篇的心跳机制的基础上 ...

  3. vue打包静态资源路径不正确的解决办法

    vue打包静态资源路径不正确的解决办法 vue项目完成打包上线的时候会碰到静态资源找不到的问题,常见的有两个 1.js,css路径不对 解决办法:打开config/index.js,将其中的asset ...

  4. Qt(自适应窗口)

    关于窗口布局: 默认控件的大小为最小尺寸: 如果此时右键设置布局时,窗口大小会自动缩放对应大小,不利于调整. 建议窗口控件设置好最小尺寸,便于窗口布局时,控件不会变形,例如: 改变minimumsiz ...

  5. Asp .net core api+Entity Framework core 实现数据CRUD数据库中(附Git地址)

    最近在学dotNetCore 所以尝试了一下api 这个功能 不多说了大致实现如下 1.用vs2017建立一个Asp.net  Core Web 应用程序 在弹出的对话框中选择 Web API 项目名 ...

  6. Core 第三组 结对作业——四则运算 Part1. Core代码编写

    结对作业——四则运算 Part1. Core代码编写 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 (因为内容较多,分为了三个部分,但作业系统中只能提交一 ...

  7. Linux实战教学笔记42:squid代理与缓存实践(一)

    第1章 Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器.这种服务器不仅可以使用户可 ...

  8. ShaderLab内置变量

    [ShaderLab内置变量]

  9. kali linux: 网卡监听及扫描网络

    一.网卡监听: kali不支持内置网卡,仅仅支持usb网卡.我用的虚拟机.首先在虚拟机的可移动设备里打开usb网卡的连接,执行ifconfig命令,如下: eth0接口是本地有线网卡信息,lo接口是本 ...

  10. Python 序列与映射的解包操作-乾颐堂

    解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...