125. Valid Palindrome
2016-05-05 09:50
253 查看
Problem
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.*
Solution
class Solution { public: bool isPalindrome(string s) { if(s.empty()) return true; int i = 0; int j = s.size()-1; while(i<j) { //skip non alphanumeric characters while(i < j && !isalnum(s[i])) ++i; //skip non alphanumeric characters while(i < j && !isalnum(s[j])) --j; //convert to lower case and compare the characters //if not the same,return false if(tolower(s[i]) != tolower(s[j])) return false; //else check next pair of characters else { ++i; --j; } } return true; } };
相关文章推荐
- Win7系统总是弹出"微软设备健康助手"窗口的故障原因及解决方法
- Java中的基础----编程规则,开发原则,面向对象设计原则、面向对象的特征
- 弹出框window.confirm()方法
- 《VR入门系列教程》之7---DK2和Crescent Bay
- 如何解决导入别人项目加载Maven库时,出现摇篮同步失败的错误?
- C++作业5
- 【深入理解JVM】:Java内存区域
- c#访问修饰符
- 大数据-java基础-5day
- 专题三 第十题
- 2-Sum问题
- C++上机作业5
- Android超高仿QQ附近的人搜索展示
- ubuntu下常用软件记录
- 学习总结 JAVA环境配置 及其相应的步骤
- php正则表达式模式修饰符
- android—资源文件(res)的引用
- intellij idea 15配置typeScript
- 在CentOS6.5上使用Jexus安装部署ASP.NET MVC4和WebApi
- 谈谈智能指针