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 ...
随机推荐
- vue2-通过axios实现数据请求
1.通过axios实现数据请求 vue.js默认没有提供ajax功能 所以使用vue的时候,一般都会使用axios的插件来实现ajax与后端服务器的数据交互. 注意,axios本质上就是javascr ...
- 【转】META标签指南:哪些meta标签该用哪些不该用?
以下内容来源:http://lusongsong.com/reed/8766.html META标签是网页代码中HEAD区的一个关键标签,其提供的信息虽然用户不可见,但却是文档的最基本的元信息.说起m ...
- c# 使用http摘要认证
.net 使用http摘要认证,返回json数据 using System; using System.Collections.Generic; using System.Linq; using Sy ...
- input框在浏览器上显示一个叉,去掉方法
/* 清除IE10及以上版本input的叉叉(X)和密码输入框的眼睛图标 */ input::-ms-clear { display: none; } input::-ms-reveal { disp ...
- 黄聪:原生js的音频播放器,兼容pc端和移动端(原创)
更新时间:2018/9/3 下午1:32:54 更新说明:添加音乐的loop设置和ended事件监听 loop为ture的时候不执行ended事件 1 2 3 4 5 6 7 8 9 10 11 12 ...
- 项目中使用package-lock.json锁版本问题
package-lock.json的作用: 锁版本,确保项目在后续拉去中,安装依赖包时依赖包的版本始终是统一的 在npm install时会自动生成package-lock.json package. ...
- Scrapy实战篇(七)之爬取爱基金网站基金业绩数据
本篇我们以scrapy+selelum的方式来爬取爱基金网站(http://fund.10jqka.com.cn/datacenter/jz/)的基金业绩数据. 思路:我们以http://fund.1 ...
- WPF设计界面不执行代码
一般在我们在设计WPF XAML界面时,XAML 引用一些后端的类.比如UserControl.Converter.MVVM,引用 xmlns:ALLUserControl="clr-nam ...
- php预定义常量
<?php echo "当前文件路径: ".__FILE__; echo "<br/>当前行数:".__LINE__; echo " ...
- JavaScript 正则表达式学习笔记
定义规则让计算机去处理字符串正则表达式写法: //第一种 var reg = new RegExp('ab'); //第二种 var reg = /ab/; 量词:{} [a-z] 从小写a-z [A ...