[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.
主要考察, 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的更多相关文章
- [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 ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 1216. Valid Palindrome III
原题链接在这里:https://leetcode.com/problems/valid-palindrome-iii/ 题目: Given a string s and an integer k, f ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- [leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
随机推荐
- VUE 初步学习
Vue 简单的总结一 Vue 简单的总结二 Vue 简单的总结三 Vue 简单的总结四(项目流程) Vue 简单的总结五 Vue(6)- Vue-router进阶.单页面应用(SPA)带来的问题 Vu ...
- 使用ffmpeg合并视频文件的三种方法
ffmpeg合并视频的方法有三种.国内大多数仅介绍了其中之一.于是觉得有必要翻译一下.其实在ffmpeg的 FAQ文档中有比较详细的说明. 使用concat协议进行视频文件的合并 这种方式的适用场景是 ...
- Adjacent Bit Counts(uvalive)
For a string of n bits x1, x2, x3,…, xn, the adjacent bit count of the string (AdjBC(x)) is given by ...
- nextcloud 安装
nextcloud 优化 设置php.ini vim /etc/php/7.0/apache2/php.ini 添加以下代码: opcache.enable= opcache.enable_cli= ...
- Mysql事务及行级锁
事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...
- ABAP内表数据和JSON格式互转
本程序演示ABAP内表数据如何转为JSON格式,以及JSON数据如何放入内表. 注:json字符串格式如:jsonstr = '[ {flag: "0",message: &quo ...
- RedHat Linux设置yum软件源为本地ISO
先挂载ISO到某个目录下(如我的:/media/RHEL_6.0 x86_64 Disc 1) # mount –o loop rhel-server-6.4-x86_64-dvd.iso /medi ...
- 10 华电内部文档搜索系统 search04
上一节我们着重讲解了权限过滤器的使用.这一节讲解一下补充一下Struts 2下面对应的Checkbox的使用.对索引下面的数据进行维护操作,一次可以删除多个.在Struts 2下面使用对应的Check ...
- 【BZOJ2780】Sevenk Love Oimaster【广义后缀自动机】
题意 给出你n个字符串和q个查询,每个查询给出一个字符串s,对于每个查询你都要输出这个字符串s在上面多少个字符串中出现过. 分析 广义后缀自动机的裸题.建好SAM以后再跑一遍得到每个状态的ocu和la ...
- Mac shell使用技巧总结(转)
1.文件操作 常用目录 /Systme/Library/Extensions // 驱动所在目录 /User/XXX/Desktop // 桌面目录 资源库 chflags nohidden ~/Li ...