valid palindrome
来源:互联网 发布:勇士vs雷霆第六场数据 编辑:程序博客网 时间:2024/03/28 21:02
题目:
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.
思路:
其实很简单,设置两个指针,头指针i 和尾指针j ,都向中间靠拢,判断有效地 s[i]和s[j]是否相等。不相等则返回false。
class Solution {public: bool isPalindrome(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s.empty()) return true; int i=0; int j=s.size()-1; while(i<=j) { if(!isalnum(s[i])) { i++; continue; } if(!isalnum(s[j])) { j--; continue; } if(s[i]==s[j]) { i++; j--; continue; } else if(isalpha(s[i]) && isalpha(s[j]) && tolower(s[i])==tolower(s[j])) { i++; j--; continue; } else return false; } return true; }};
Run Status: Accepted!
Program Runtime: 52 milli secs
Program Runtime: 52 milli secs
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- valid palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- dm37xx android2.3.4增加recovery升级功能(二)——调试
- IOS atomic与nonatomic,assign,copy与retain的定义和区别
- 分割字符串(strsplit) 二
- 对ORA-01795: 列表中的最大表达式数为 1000的处理(算法:计算数量及切割)
- 轻量级语言Lua入门
- valid palindrome
- git work_cmd
- Qt Creator 代码自动补全设置
- struts2集成fckeditor(来自大型门户网站是这样练成的一书)
- jQuery验证控件jquery.validate.js使用说明+中文API
- poj 1094 Sorting It All Out (拓扑排序)
- Sqlite 扩展功能 GET_PHONEBOOK_INDEX
- Oracle笔记(五) 单行函数
- joj 1113 The Game