[leetcode-125]Valid Palindrome(c++)
2015-08-12 16:07
393 查看
问题描述:
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.
分析:注意过滤就好了。
代码如下:12ms
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.
分析:注意过滤就好了。
代码如下:12ms
[code]class Solution { public: char getValid(char s){ if(s>='A'&&s<='Z') return s+32; else if(s>='a'&&s<='z') return s; else if(s>='0'&&s<='9') return s; else return 0; } bool isPalindrome(string s) { int length = s.size(); int start = 0; int end = length-1; while(start<end){ char sa = s[start]; char ea = s[end]; char ss = getValid(sa); if(!ss){ start++; continue; } char ee = getValid(ea); if(!ee){ end--; continue; } if(ss==ee){ start++; end--; }else{ return false; } } return true; } };
相关文章推荐
- C语言实现多级反馈队列调度算法
- 使用cmake编译C语言项目时使用第三方库
- C++实现操作系统调度算法(FSFS,SJF,RR,多级反馈队列算法)
- 黑马程序员——C语言中变量的存储类别
- c语言可变参数编程
- hdu1198 二维并查集
- C/C++语言 void 及 void 指针深层探索
- [leetcode-124]Binary Tree Maximum Path Sum(c++)
- VC++对话框中加状态栏
- Android------使用C/C++调用SO库
- C/C++中如何获取数组的长度?
- C++ 对象数组
- 【C++实现】HeadFirst策略模式设计模式
- C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
- C++ ostream与printf比较
- C语言做一个通讯录程序(在console里面运行)
- c++有符号变量和无符号变量相加的结果
- C语言关于补码的解释及误区
- 【c++编程思想学习笔记】operator=为何要检查自赋值的解答
- 【c++编程思想学习笔记】解决浅拷贝的两种方法:(深拷贝)(引用计数+写拷贝)