[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。

 1 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int left=0,right =s.size()-1;
 5         while(left<right)
 6         {
 7             if(!isalnum(s[left]))    
 8                 left++;
 9             else if(!isalnum(s[right]))
10                 right--;
11             else
12             {
13                 if(tolower(s[left])==tolower(s[right]))
14                 {
15                     left++;
16                     right--;
17                 }
18                 else
19                     return false;
20             }
21         }
22         return true;        
23     }
24 };

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

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

函数名function description函数说明
isalnumCheck if character is alphanumeric (function )查看参数是否为字符或数字
isalphaCheck if character is alphabetic (function )查看参数是否为字符
isblank Check if character is blank (function )查看参数是否为空格
iscntrlCheck if character is a control character (function )查看参数是否为控制字符
isdigitCheck if character is decimal digit (function )查看参数是否为十进制数字
isgraphCheck if character has graphical representation (function )查看参数是否为可显示字符
islowerCheck if character is lowercase letter (function )查看参数是否为可打印字符
isprintCheck if character is printable (function )查看参数是否为小写字符
ispunctCheck if character is a punctuation character (function )查看参数是否为标点符号
isspaceCheck if character is a white-space (function )查看参数是否为空字符
isupperCheck if character is uppercase letter (function )查看参数是否为大写字符
isxdigitCheck if character is hexadecimal digit (function )查看参数是否为十六进制数字

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

ASCII valuescharactersiscntrlisblankisspaceisupperislowerisalphaisdigitisxdigitisalnumispunctisgraphisprint
x00 .. 0x08NUL, (other control codes)x           
0x09tab ('\t')xxx         
0x0A .. 0x0D(white-space control codes:'\f','\v','\n','\r')x x         
0x0E .. 0x1F(other control codes)x           
0x20space (' ') xx        x
0x21 .. 0x2F!"#$%&'()*+,-./         xxx
0x30 .. 0x39123456789      xxx xx
0x3a .. 0x40:;<=>?@         xxx
0x41 .. 0x46ABCDEF   x x xx xx
0x47 .. 0x5AGHIJKLMNOPQRSTUVWXYZ   x x  x xx
0x5B .. 0x60[\]^_`         xxx
0x61 .. 0x66abcdef    xx xx xx
0x67 .. 0x7Aghijklmnopqrstuvwxyz    xx  x xx
0x7B .. 0x7E{|}~         xxx
0x7F(DEL)x           

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

转载于:https://www.cnblogs.com/double-win/p/3780717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值