leetcode 125 —— Valid Palindrome
2015-08-11 17:14
453 查看
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.
思路:双端扫描,有非字母数字就跳过。
class Solution {
public:
bool isPalindrome(string s) {
if (s.empty()) return true;
int l = 0;
int r = s.size() - 1;
while (l <= r){
while (l<=r&&!isalnum(s[l]))
l++;
while (l <= r&&!isalnum(s[r]))
r--;
if (l > r) break;
if (s[l] == s[r]||s[l]+32==s[r]||s[l]==s[r]+32){
l++;
r--;
}
else
return false;
}
return true;
}
};
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 isPalindrome(string s) {
if (s.empty()) return true;
int l = 0;
int r = s.size() - 1;
while (l <= r){
while (l<=r&&!isalnum(s[l]))
l++;
while (l <= r&&!isalnum(s[r]))
r--;
if (l > r) break;
if (s[l] == s[r]||s[l]+32==s[r]||s[l]==s[r]+32){
l++;
r--;
}
else
return false;
}
return true;
}
};
相关文章推荐
- gnuplot使用的一些技巧
- 上市公司行情查询网站
- 喵哈哈村的挑衅
- Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport e
- Hama框架学习(三) Master如何将job规划到GroomServer
- C++11中变量初始化方法汇总
- 26-网络编程-05-网络编程(IP对象_InetAddress)
- LIBSVM数据格式及其使用
- Container with most water
- 学习zepto.js(原型方法)[2]
- MySQL数据库性能优化专题
- 行内元素和块级元素
- NOIP2009 提高组 最优贸易 解题报告
- 正则表达式介绍
- AsyncTask执行顺序
- ClickOnce部署
- Android 网络学习之使用多线程下载,支持断点续传
- C++primer【笔记】 顺序容器
- 26-网络编程-03-网络编程(网络要素-端口)
- 26-网络编程-04-网络编程(网络要素-传输协议)