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的更多相关文章

  1. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  2. 125. Valid Palindrome判断有效的有符号的回文串

    [抄题]: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

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

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

  4. leetcode 125. Valid Palindrome ----- java

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

  5. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  6. Java [Leetcode 125]Valid Palindrome

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

  7. 【LeetCode】125. Valid Palindrome

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

  8. 【一天一道LeetCode】#125. Valid Palindrome

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. [leetcode]125. Valid Palindrome判断回文串

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

随机推荐

  1. .NET技术-6.0. Expression 表达式树 生成 Lambda

    .NET技术-6.0. Expression 表达式树 生成 Lambda public static event Func<Student, bool> myevent; public ...

  2. python 用 pycharm 光速下载各种包

    https://blog.csdn.net/z1178517021/article/details/80200999 就是这么简单

  3. 2.react 基础 - create-react-app 目录结构 及 组件应用

    1. react-app 脚手架的 目录结构 node_modules -d 存放 第三方下载的 依赖的包 public -d    资源目录 favicon.ico - 左上角的图标 index.h ...

  4. 91.一次性处理多条数据的方法:bulk_create,update,delete

    (1)bulk_create: 可以一次性的创建多个对象 示例代码如下: from django.http import HttpResponse from .models import Pulish ...

  5. maven项目出现Xxx is not a Servlet的问题

    应该是tomcat的jar包和maven的jar包冲突 在pom.xml中找到 <dependency> <groupId>org.apache.tomcat</grou ...

  6. 一、早期(Early Stage)

    一.早期(Early Stage) 如果单纯从零基础开始,早期(Early Stage)应该是一到两个月(由于英语与中文差异比与其他语言大,中国同学至少两个月,但也不应过长.我们的经验是一般中国同学会 ...

  7. Python—冒泡排序算法

    冒泡排序 一,介绍 冒泡排序(Bubble Sort)也是一种简单直观的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再 ...

  8. CodeForces 990B Micro-World(思维、STL)

    http://codeforces.com/problemset/problem/990/B 题意: 有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且a ...

  9. 蓝桥杯 K好数(dp)

    Description 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.1 ...

  10. 七、Shell脚本高级编程实战第七部

    一.写网络服务的系统启动脚本 利用case语句开发类似系统启动rsync服务的脚本 代码: #!/bin/sah. /etc/init.d/functionspidfile="/var/ru ...