LeetCode (16) Valid Palindrome (回文)
来源:互联网 发布:矩阵理论教材推荐 编辑:程序博客网 时间:2024/05/23 07:25
题目描述
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.
本题即英文版的“回文”,也就是一个字符串的字符顺序正向和反向遍历是一样的。本题要求的字符串只需要考虑字母和数字。题目的解法即通过使用两个指针/index,同时从前向后和从后向前遍历,并比较有效字母(字母和数字)是否相同。
class Solution {public: bool isAlphaNum(char c) { if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true; else if (c >= '0' && c <= '9') return true; else return false; } bool isPalindrome(string s) { int i = 0, j = s.size() - 1; while(i<j) { while(i < s.size() && !isAlphaNum(s[i])) i++; while(j >= 0 && !isAlphaNum(s[j])) j--; if (i < j && tolower(s[i]) != tolower(s[j])) return false; i++; j--; } return true; }};
0 0
- LeetCode (16) Valid Palindrome (回文)
- leetcode:Valid Palindrome 合法回文
- LeetCode - Valid Palindrome (判断“回文”)
- [LeetCode]—Valid Palindrome 回文判断
- 【LeetCode】- Valid Palindrome(正确的回文)
- leetcode 刷题:Valid Palindrome(回文字符串)
- (LeetCode)Valid Palindrome --- 判断回文字符串
- leetcode 125. Valid Palindrome | 回文string
- LeetCode Valid Palindrome(验证回文字符串)
- Leetcode-680. Valid Palindrome II(判断回文)
- [leetcode]16 Valid Palindrome
- [LeetCode] Palindrome Number & Valid Palindrome - 回文系列问题
- Valid Palindrome 回文
- 回文判定 Valid Palindrome
- 【字符串回文】Valid Palindrome
- Valid Palindrome有效回文
- Valid Palindrome(回文字符串)
- Valid Palindrome 检查是否是有效的回文 @LeetCode
- android sqlite 操作:rawQuery and execSQL
- 深入学习Memcached
- 法向量工具类
- hdu 5093 Battle ships(二分匹配)
- static和extern对变量和函数的作用
- LeetCode (16) Valid Palindrome (回文)
- 直方图常用函数简介(C++)
- MTK Android Driver知识大全
- zoj 2975 Kinds of Fuwas(超时题)
- Oracle数据库学习笔记四——存储过程的值传递和引用传递
- 数据库上机作业4
- 括号匹配(二) 区间DP
- iOS截图
- HDU 3191 + HDU 1688(次短路)