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 ...
随机推荐
- SqlServer索引及优化详解
实际上,您可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonclustered index ...
- Linux 学会这些基本可以啦
1,Linux 内壳文件:cat /etc/issue OR /etc/redhat-release ubuntu[apt-get install -y vim] centos[yum install ...
- MySql指令集
http://blog.csdn.net/cl05300629/article/details/9464007
- pull刷新
package com.example.mylist; import java.util.ArrayList; import java.util.List; import com.example.ad ...
- 拿到新机器,进行初始化和部署Nginx的过程
1. 在/etc/ansbile/hosts中添加主机init 2. 在sysinit.yml中修改要初始化的机器: hosts: init 3. 设置不检查key export ANS ...
- HTML编码规则、CSS属性声明顺序--简介
From AmazeUI:http://amazeui.org/getting-started/html-css-guide HTML 属性顺序 HTML 属性应当按照以下给出的顺序依次排列,确保代码 ...
- maven .assembly
配置文件中 配置好Assemblyc插件. 功能:打依赖jar包. java代码如下: <assembly xmlns="http://maven.apache.org/plugins ...
- windows server 2008 r2 切换windows 7主题方法
1. 打开Powershell 里 Cmdlets 管理角色和功能Import-Module servermanager 2. 安装桌面体验Add-WindowsFeature Desktop-Exp ...
- C#与XML Schema的问题
http://bbs.csdn.net/topics/50493564 weileily: 用XmlSchema.Read方法读取了一个xsd文件,请问如何遍历检索器中的ComplexType与Sim ...
- Jena TDB 101 Java API without Assembler
Update on 2015/05/12 ongoing tutorials site on https://github.com/zhoujiagen/semanticWebTutorialUsin ...