leetcode:Valid Palindrome 合法回文
2014-11-17 12:53
239 查看
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.
这题的意思就是判断一串字符串是否是回文,只考虑字母数字的情况。
alphanumeric 是字母数字的意思,我一开始以为是字母,看我写的那个判断函数名就知道了
,结果可想而知,深深的感到英语这厮对我充满了恶意,
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.
这题的意思就是判断一串字符串是否是回文,只考虑字母数字的情况。
alphanumeric 是字母数字的意思,我一开始以为是字母,看我写的那个判断函数名就知道了
,结果可想而知,深深的感到英语这厮对我充满了恶意,
class Solution { public: bool isPalindrome(string s) { int tail = s.length() -1 ; int head = 0; while (head < tail) { while (!isAlpha(s[tail]) && tail>head) --tail; while (!isAlpha(s[head]) && head<tail) ++head; if ( tolower(s[head]) != tolower(s[tail])) return false; ++head; --tail; } return true; } private: bool isAlpha( char c) { if(('A'<=c && c<='Z')||('a'<=c && c<='z')|| ('0'<=c && c<='9')) return true; return false; } };
相关文章推荐
- LeetCode-Valid Palindrome-合法回文
- leetcode (9) - Palindrome Number 整数回文
- LeetCode 131. Palindrome Partitioning(回文分区)
- leetcode Longest Palindromic Substring最长回文子串
- [Leetcode] valid palindrome 验证回文
- leetcode题集——最长回文子串(待整理)
- 【leetcode】第9题:判断整型数是否为回文数
- [LeetCode] 409. Longest Palindrome 最长回文
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
- LeetCode 32 Longest Valid Parentheses 最大合法括号匹配长度计算 动态规划算法有待学习
- [Leetcode] 回文问题
- Leetcode 9. Palindrome Number(判断回文数字)
- leetcode : 336. Palindrome Pairs : 拼接回文的个数
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
- LeetCode 214. Shortest Palindrome(最短回文)
- [Leetcode] Longest palindromic substring 最长回文子串
- (LeetCode)Valid Sudoku --- 合法数独
- LeetCode: Palindrome 回文相关题目
- LeetCode 5 : Longest Palindromic Substring ---- 最长回文
- leetcode 5 Longest Palindromic Substring--最长回文字符串