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 ...
随机推荐
- java 重载、重写、重构的区别
1.重载 构造函数是一种特殊的函数,使用构造函数的目的是用来在对象实例化时初始化对象的成员变量.由于构造函数名字必须与类名一致,我们想用不同的方式实例化对象时,必须允许不同的构造方法同时存在,这就用到 ...
- Qt applendPlainText()/append() 多添加一个换行解决方法
Qt applendPlainText()/append() 多添加一个换行解决方法 void ConsoleDialog::appendMessageToEditor(const QString & ...
- pll时钟延迟为问题
pll时钟延迟为问题 这关系到pll的工作方式,如果pll内部使用的是鉴频器,则输入和输出将没有固定的相位差,就是每次锁定都锁定在某个相位,但每次都不一样.如果使用的是鉴相器,则输入和输出为0相位差. ...
- Windows下安装Spark环境
根据博客总结 https://blog.csdn.net/nxw_tsp/article/details/78281533 需要的安装软件可以在网盘下载: 链接:https://pan.baidu.c ...
- Python语法进阶
1.变量进阶 2.局部变量.全局变量 3.函数进阶 4.函数进阶
- 系统右键菜单(级联菜单)资料--cascading menus
通过RegistryKey 简单的实现单级菜单 http://www.cnblogs.com/sjcatsoft/archive/2009/02/25/1398203.html 通过subcomman ...
- Java中的“==操作符”和equals方法有什么区别
Java中的"=="和equals方法究竟有什么区别? 1.==操作符 "=="操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的 ...
- 转载 修改oracle用户密码永不过期
1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码 ...
- spark2.1源码分析4:spark-network-common模块的设计原理
spark-network-common模块底层使用netty作为通讯框架,可以实现rpc消息.数据块和数据流的传输. Message类图: 所有request消息都是RequestMessage的子 ...
- golang初识2
1. 赋值与申明 str := "Hello, WebAssembly" // 简短声明 标准格式: var str string str = "Hello, WebAs ...