[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 ...
随机推荐
- 在线浏览office 文件
http://blog.csdn.net/binyao02123202/article/details/20051683 [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有 ...
- **深入了解lambda
之前已经了解过lambda了,但是在学习了闭包之后,我们有必要在探讨一下lambda(匿名函数). 匿名函数本质上就是一个函数,它所抽象出来的东西是一组运算. 它的使用场景就是:你在某处就真的只需要使 ...
- 跟我学算法-tensorflow 实现神经网络
神经网络主要是存在一个前向传播的过程,我们的目的也是使得代价函数值最小化 采用的数据是minist数据,训练集为50000*28*28 测试集为10000*28*28 lable 为50000*10, ...
- margin+absolute布局:右栏固定主内容自适应 demo
margin+absolute布局:右栏固定主内容自适应 demo 头部 Aside侧边栏区域 Main主内容区域 底部 #demo{width:80%;margin:auto;height:300p ...
- Fatal error: Class 'MongoDB\Driver\Manager' not found
折腾了好久,总算找到了问题所在! 首先!!检查你安装的PHP拓展版本是否正确,能在在phpinfo()中看到拓展,若看不到,则安装错误! 其次,我在安装PHP扩展的时候,安装的是mongo拓展,如下图 ...
- 如何显示当前Mipmap级别?
[如何显示当前Mipmap级别?] 乘以 mainTextureSize/mipTextureSize是为了让mipColorsTexture纹理与mainTexture级别对应.直接用uv是不行的, ...
- centos6.5系统hadoop2.7安装sqoop
一.sqoop简介 Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL ...
- ubuntu14.04 64 位 vmware tools 问题2
当提示说open-vm-tools版本太低时可以这样解决 0.使用最新版本12.5的vmware player. 1.sudo apt-get autoremove open-vm-dkms open ...
- ICO流程,casestudy
https://medium.com/crypto-oracle/ico-analysis-framework-nex-case-study-bf65586b4b32
- libpcap编程实例
#include <stdio.h> #include <stdlib.h> #include <pcap.h> #include <errno.h> ...