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.

验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。但是这里,加入了空格和非字母数字的字符,增加了些难度,但其实原理还是很简单:只需要建立两个指针,left和right, 分别从字符的开头和结尾处开始遍历整个字符串,如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写。等左右指针都找到字母数字时,比较这两个字符,若相等,则继续比较下面两个分别找到的字母数字,若不相等,直接返回false.

时间复杂度为O(n), 代码如下:

var isPalindrome = function(s) {
var n = s.length;
if( n <= 1){
return true
}
var left = 0;
var right = n -1;
while(left < right){
if(!isAlphanumeric(s[left])){
left++
}else if(!isAlphanumeric(s[right])){
right--
}else if(s[left].toLowerCase() !== s[right].toLowerCase()){
return false
}else{
left++
right--
}
}
return true
};
function isAlphanumeric(a){
var c = a.charCodeAt(0)
if( c >= 48 && c<=57){//0-9
return true
}
if( c >= 65 && c<= 90){//A-Z
return true
}
if( c >= 97 && c<= 122){//a-z
return true
}
return false
}

leetcode125. Valid Palindrome的更多相关文章

  1. [Swift]LeetCode125. 验证回文串 | Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  4. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  7. 25. Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  8. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. Valid Palindrome [LeetCode]

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. Rabbit MQ

    前言: MQ 是什么?队列是什么,MQ 我们可以理解为消息队列,队列我们可以理解为管道.以管道的方式做消息传递. 场景: 1.其实我们在双11的时候,当我们凌晨大量的秒杀和抢购商品,然后去结算的时候, ...

  2. MySQL is running but PID file could not be found

    在Linux 中,当你启动或者重启 MySQL 时,报 关于 PID file 的错误 解决方法 第一步:找到   mysql 中 data 目录下的 mysql-bin.index 文件,然后删除 ...

  3. update20181214 - uGetHttpData.pas

    function DecodePJItem(sText: string): TList<TDataItem>; var reg: TRegEx; mc: TMatchCollection; ...

  4. S50 抓取pattern数据

    S50(原V50) 测试机台湾久元电子研发的一款数字芯片测试系统,行业内有很多人使用: 现在记录下S50抓取pattern数据的一些方法: 程序主要是通过read_log配合c代码实现,pattern ...

  5. JSON AST 生成MD

    使用 JsonLite 获取 JSON  AST class Program { static void Main(string[] args) { string fileName = $" ...

  6. 【SQL实践】其他常用SQL汇总

    [SQL实践]其他常用SQL汇总 1.联表更新 update students stu inner join course on course.STUDENT_ID=stu.id set stu.na ...

  7. 【如皋OJ】1127:正整数N转换成一个二进制数

    1127: 正整数N转换成一个二进制数 时间限制: 1 Sec  内存限制: 128 MB提交: 85  解决: 59[提交] [状态] [讨论版] [命题人:zhuzhigang] 题目描述 输入一 ...

  8. 用最简单的话告诉你什么是ElasticSearch

    介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 L ...

  9. VMware与Centos系统安装

    Linux介绍 1. Linux Linux和windows一样都是操作系统,Linux是开源的.免费的.自由传播的类Unix操作系统软件. 是一个基于POSIX和UNIX的多用户.多任务.支持多线程 ...

  10. IoC容器的接口设计

    1.从接口BeanFactory---HierarchicalBeanFactory---ConfigurableBeanFactory,是一条主要的BeanFactory设计路径. 2.第二条接口设 ...