leetcode - Valid Palindrome
2015-06-22 20:25
295 查看
leetcode - Valid Palindrome
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.
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.
class Solution { public: bool isAlphanumeric(const char a){ if( (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9' )) return true; else return false; } bool isPalindrome(string s) { if(s.size()==0) return true; string::iterator it_beg = s.begin(); string::iterator it_end = s.end(); //bool flag = true; //while(!isAlphanumeric(*it_beg) && it_end!=s.end()) it_beg++; //while(!isAlphanumeric(*it_end) && it_end!=s.begin()) it_end--; while(it_beg < it_end){ if(!isAlphanumeric(*it_beg)) it_beg++; else if(!isAlphanumeric(*it_end)) it_end--; else if(*it_beg == *it_end || (*it_beg -*it_end == 'a' - 'A') || (*it_beg -*it_end == 'A' - 'a') ){ it_beg++; it_end--; } else{ return false; } } return true; } };
相关文章推荐
- 样本方差:为嘛分母是n-1
- C实现 LeetCode->Partition List(双指针大法)(单链表)
- 第14章 C++代码重用
- js中的this
- 软件绩效评分
- Python简单对文件进行读写操作
- Oracle数据库之PL/SQL触发器
- bzoj2243: [SDOI2011]染色
- rhel源更换为centos源
- java4Android(14)I/O流 、分类、目的、字节流重要类及方法FileInputStream
- 黑马程序员——初识JAVA
- 用场景来规划测试工作
- Android ListView侧边字母索引栏SideBar控件的实现
- 免费的在线源码控制网站介绍
- 异常:HRESULT: 0x80070057 (E_INVALIDARG) 的处理
- 【CF542E】Playing on Graph
- 自动生成MVC中的Service层、Dao层及Model层
- 批量替换sql 字段名称sql语句
- linux中gdb的使用方法
- 黑马程序员---iOS编程效率提高必备