Valid Palindrome
来源:互联网 发布:南京厚蕴网络 编辑:程序博客网 时间:2024/04/25 20:08
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 alphanumeric (char c) { return (c >= 'a' && c <= 'z' || c >= '0' && c <= '9'); } void tolower(string &s) { for(int i = 0; i < s.size(); ++i) if(s[i] <= 'Z' && s[i] >= 'A') s[i] += 32; } bool isPalindrome(string s) { if(s.size() <= 1) return true; int i = 0; int j = s.size() - 1; tolower(s); while(i <= j) { if(alphanumeric(s[i]) && alphanumeric(s[j]) && s[i] == s[j]) {++i; --j;} else if(!alphanumeric(s[i])) ++i; else if(!alphanumeric(s[j])) --j; else return false; } return true; }};
1 0
- 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
- syntax: command not found
- 修复U盘
- Jena TDB的使用简介
- 极大似然估计
- 搜索份额之“皇帝的新衣”闹剧
- Valid Palindrome
- R中的极大似然估计
- CF/379/problem/C
- JAVA沙箱安全总结
- Win764位 vs2012开发安卓 环境搭建
- 电影爱好者的R函数
- MATLAB基本操作(六):矩阵操作的相关函数
- R中的距离与聚类
- iOS - Applications must be ready for upload on iTunes Connect before they can be validated or submit