25. Valid Palindrome
Valid Palindrome
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.
思想:简单的从两端来逐个读字符,判断是否相等。(36ms)
inline char getLowerChar(string &s, int id) {
if((s[id] >= 'a' && s[id] <= 'z') || (s[id] >= '0' && s[id] <= '9')) return s[id];
else if(s[id] >= 'A' && s[id] <= 'Z') return s[id] + 32;
else return 0;
}
inline char getFirstChar(string &s, int& l) {
while(l < s.size()) {
char ch = getLowerChar(s, l);
if(ch) return ch;
++l;
}
return '*';
}
inline char getLastChar(string &s, int& h) {
while(h >= 0) {
char ch = getLowerChar(s, h);
if(ch) return ch;
--h;
}
return '#';
}
class Solution {
public:
bool isPalindrome(string s) {
int l = 0, h = s.size()-1;
while(l <= h) {
if(getFirstChar(s, l) != getLastChar(s, h))
return false;
++l, --h;
}
return true;
}
};
25. Valid Palindrome的更多相关文章
- LeetCode 之 Valid Palindrome(字符串)
[问题描写叙述] Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
随机推荐
- hadoop运行原理之shuffle
hadoop的核心思想是MapReduce,但shuffle又是MapReduce的核心.shuffle的主要工作是从Map结束到Reduce开始之间的过程.首先看下这张图,就能了解shuffle所处 ...
- HTTP 304
304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...
- html a 链接标签title属性换行鼠标悬停提示内容的换行效果
鼠标经过悬停于对象时提示内容(title属性内容)换行排版方法,html title 换行方法总结. html的title属性默认是显示一行的.如何换行呢? 这里DIVCSS5总结介绍两种换行方法为大 ...
- 关于HTTP的几种
301.404.200.304等HTTP状态,代表什么意思? 如果某项请求发送到您的服务器要求显示您网站上的某个网页(例如,用户通过浏览器访问您的网页或 Googlebot 抓取网页时),服务器将会返 ...
- html,if标签使用
$vo.auth_id eq $vo2.auth_pid报错不能用 condition里面访问变量,不能用点的形式,应该用['']的形式访问
- POJ-1182 分组并查集
今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...
- 监听调试web service的好工具TCPMon
监听调试web service的好工具TCPMonhttp://ws.apache.org/commons/tcpmon/download.cgi TCPMon Tutorial Content In ...
- CI框架引入外部css和js文件
首先在项目根目录下建立assets文件夹,在这个文件夹下再建立css和js文件夹分别放置css和js文件 然后,在项目根目录下建立.htaccess文件 内容如下: RewriteEngine on ...
- linux,python 常用的处理log的命令
一般的log文件都是需要过滤 ps:管道符| 管道符前面的输出值 grep 过滤查找 将是error的log过滤显示 grep '221.2.100.138' web.access.log gr ...
- H5版定点投篮游戏(1)--物理模型抽象
前言: 前几天目睹了大学同学开了个微店, 算是间接体验微信公众平台的使用. 觉得非常便捷和方便, 于是自己也想捣鼓一个. 公众号取名: "木目的H5游戏世界", 定位做成一个, 个 ...