LeetCode *** 125. Valid Palindrome
2016-04-10 20:48
260 查看
题目:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
分析:
因为题目没读明白以及自己粗心把j写成了i导致错了4次。唉,经常犯这些幼稚的错误。
代码:
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.
分析:
因为题目没读明白以及自己粗心把j写成了i导致错了4次。唉,经常犯这些幼稚的错误。
代码:
class Solution { public: bool isPalindrome(string s) { int i=0,j=s.length()-1; while(i<j){ if((isalpha(s[i])||isdigit(s[i]))&&(isalpha(s[j])||isdigit(s[j]))){ if(isalpha(s[i]))s[i]=tolower(s[i]); if(isalpha(s[j]))s[j]=tolower(s[j]); if(s[i]!=s[j])return false; else {i++;j--;} } else { if(!(isalpha(s[i])||isdigit(s[i])))i++; if(!(isalpha(s[j])||isdigit(s[j])))j--; } } return true; } };
相关文章推荐
- 关于hibernate使用log4j
- linux ssh -l 命令运用
- 网络基本功系列:细说交换机
- 数据库下载
- [置顶] MySQL5.7.9安装
- 快速排序的实现(两种)
- 【数位DP】[CQOI2016]手机号码
- Centos7 Ansible安装
- libcudnn.so.6.5:cannot open sharedobject file: No such file or directory
- 谁是真泛型
- Android - 十分钟理解手机适配 与 lib工程生成包
- 第六周进度报告
- 第四次作业
- 排序注意事项
- 常用的布局和View常用属性
- Eclipse更改主题,换个黑色的背景颜色吧,哈哈
- Ubuntu14.04安装nodejs
- 数据库的恢复
- [LeetCode-91] Decode Ways
- 高效使用 STL