[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 ...
随机推荐
- G++ 4.4.7 无法编译模板程序,Vs可以,和解?智者尾部留言,本人第一次使用vs pro,通常并且习惯在linux下写些小东西,虽然程序简单;
vs 模板编译运行Ok \ linux g++ 4.4.7编译模板测试程序,报无法定义 template <typename or class 中的 AnyType> 类型的数据 Exam ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- MySQL学习笔记之二---引擎介绍MyISAM VS InnoDB
前言 MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一 ...
- Redis 主从同步配置
主从功能: 为了防止 Redis 磁盘损坏,导致数据丢失,Redis 提供了复制功能,将一个主数据库的数据自动同步到从数据库,防止数据丢失. 同时还可以配置一主多从来分担主压力,主只接受写的操作,将读 ...
- easyui datagrid combobox下拉框获取数据问题
最近在使用easyui的datagrid,在可编辑表格中添加一个下拉框,查了下API,可以设置type : 'combobox',来做下拉框,这下拉框是有了,可是这后台数据怎么传过来呢,通过查API可 ...
- linux运维笔记——curl
** 1.获取网站返回码 ** [root@Cacti ~]# curl -I www.qq.com HTTP/1.1 200 OK Server: squid/3.4.1 Date: Wed, 08 ...
- Spark 性能相关参数配置详解-压缩与序列化篇
随着Spark的逐渐成熟完善, 越来越多的可配置参数被添加到Spark中来, 本文试图通过阐述这其中部分参数的工作原理和配置思路, 和大家一起探讨一下如何根据实际场合对Spark进行配置优化. 由于篇 ...
- 安装运行 rovio odometry
https://github.com/ethz-asl/rovio https://github.com/ethz-asl/kindr 1. 新建catkinworkspace mkdir -p ~/ ...
- nginx相关教程
1.nginx简易的教程 http://www.cnblogs.com/jingmoxukong/p/5945200.html#%E8%B7%A8%E5%9F%9F%E8%A7%A3%E5%86%B3 ...
- [Training Video - 6] [File Reading] Using log object in the Groovy class
Car c= new Car(log); c.print() class Car{ def log public Car(log){ this.log=log } public void print( ...