leetcode125. 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.
验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如“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的更多相关文章
- [Swift]LeetCode125. 验证回文串 | 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 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 ...
- 25. Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [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 ...
随机推荐
- 18.14 构建Linux根文件系统
18.14.1 Busybox1.7.0之init程序分析 1.读取配置文件 2.解析配置文件 3.执行用户程序(根据配置文件中指定的内容) 配置文件: 1.指定应用程序 2.何时执行 busybox ...
- Mysql 一个表中的数据插入另一个表中
两张表的字段一致,并且插入全部数据 INSERT INTO 目标表 SELECT * FROM 来源表 ; 例如,要将 articles 表插入到 newArticles 表中,则可以通过如下 ...
- jquery: 获取当前天加减一天
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- SQL优化经验
SQL 优化经验总结34条 我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效 ...
- oracle死锁的处理办法
摘自:https://www.cnblogs.com/xuke/p/4053396.html http://blog.itpub.net/30036720/viewspace-2121034/ ora ...
- 1.编写一个shell脚本
一.shell和shell脚本 在linux系统下,以 #/bin/bash开头的文本会被shell解释器进行解释. shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操 ...
- 一、新建springBoot项目
三种方式新建SpringBoot项目: 官网, myeclipse, idea 1.官方网站新建(https://start.spring.io/) 1)打开官网,选择自己需要的springBo ...
- note10 元组
元组 Tuple +元组即不可变(immutable)列表 除了可改变列表内容的方法外,其他方法均适用于元组 因此,索引.切片.len().print等均可用 但是,appeng.extend.del ...
- (简单)华为M3青春 CPN-AL10的Usb调试模式在哪里打开的步骤
每次我们使用PC通过数据线连接到安卓手机的时候,如果手机没有开启usb开发者调试模式,PC则没能成功检测到我们的手机,有时候我们使用的一些功能比较强的的应用软件比如以前我们使用的一个应用软件引号精灵, ...
- Python课程第一天作业
一.第一题:简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型? 计算机是不能理解高级语言的,更不能直接执行高级语言,它只能直接理解机器语言,所以使用任何高级语言编写的 ...