[LC] 125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if s is '':
return True
import string
valid = set(string.ascii_letters + string.digits)
s_cp = s[:].lower()
left, right = 0, len(s) - 1
while left <= right:
if s_cp[left] not in valid:
left += 1
elif s_cp[right] not in valid:
right -= 1
elif s_cp[left] != s_cp[right]:
return False
else:
left += 1
right -= 1
return True
class Solution {
public boolean isPalindrome(String s) {
if (s.length() == 0) {
return true;
}
char[] charArr = s.toCharArray();
int left = 0, right = s.length() - 1;
while (left < right) {
if (!Character.isLetterOrDigit(charArr[left])) {
left += 1;
} else if (!Character.isLetterOrDigit(charArr[right])) {
right -= 1;
} else if (Character.toUpperCase(charArr[left]) != Character.toUpperCase(charArr[right])) {
return false;
} else {
left += 1;
right -= 1;
}
}
return true;
}
}
[LC] 125. Valid Palindrome的更多相关文章
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- 125. Valid Palindrome判断有效的有符号的回文串
[抄题]: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- leetcode 125. Valid Palindrome ----- java
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- Java [Leetcode 125]Valid Palindrome
题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【LeetCode】125. Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 【一天一道LeetCode】#125. Valid Palindrome
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- Asp.NET CORE安装部署
先安装IIS再安装这两个,不然后面各种bug HTTP 错误 500.19 代码 0x8007000d 解决方案 for win7_64 asp.net core IIS Web Core 1.比如最 ...
- VUE- iView组件框架的使用
VUE- iView组件框架的使用 1. 下载iView 工程. 引用:https://www.iviewui.com/
- 2)将普通工程变成动态库dll
1)打开那个工程: 2)然后 看属性里面的控制平台:
- 【转帖】虚拟化Pod性能比裸机还要好,原因竟然是这样!
虚拟化Pod性能比裸机还要好,原因竟然是这样! http://www.itpub.net/2020/02/27/5340/ 其实感觉 linux也可以做到 NUMA的节点优化 其实 直接在 ESXi上 ...
- OA|DOAJ|Highwire press|Springeropen|Plos journal|电子印本|中国科技论文在线|arxiv|chinaxiv|MIT机构知识库|中科院机构知识库|Email alert|Citeseer|RSS|F1000 prime
信息检索 OA:open access开放获取 金色OA:出版社主导, 开放出版,全部都可以下载. 开放论文:只有部分可以下载. 绿色OA:作者主导,发表后放在机构知识库中,排版不同,但是内容一致.E ...
- Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员、局部),匿名对象的调用,构造代码块(5)
Java面向对象(概述,构造函数,类与对象的关系,this关键字,成员.局部),匿名对象的帝爱用,构造代码块(5)
- ASP.NET ZERO 学习 导航菜单
定义PageNames和PermissionName PageNames : Web/App_Start/Navigation/PageNames.cs public const string Das ...
- php的date()函数判断今天是星期几
d 月份中的第几天,有前导零的 2 位数字 01 到 31 D 星期中的第几天,文本表示,3 个字母 Mon 到 Sun j 月份中的第几天,没有前导零 1 到 31 l ("L&q ...
- java和数据库中所有的锁都在这了
1.java中的锁 1.1 锁的种类 公平锁/非公平锁 可重入锁/不可重入 独享锁/共享锁 读写锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 1.2 锁详细介绍 1.2.1 公平锁,非公平锁 公平锁 ...
- python——print函数
.print()函数概述 print() 方法用于打印输出,是python中最常见的一个函数. 该函数的语法如下: print(*objects, sep=' ', end='\n', file=sy ...