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. Rancher安装 - CentOS7(Docker)环境

    Rancher安装 - CentOS7(Docker)环境 对于开发和测试环境,我们建议通过运行单个Docker容器来安装Rancher.在此安装场景中,您将在单个Linux主机上安装Docker,然 ...

  2. P1618 三连击(升级版)

    题解: #include<stdio.h>int main(){ int A,B,C; scanf("%d %d %d",&A,&B,&C);  ...

  3. [RoarCTF 2019]Easy Java

    0x01知识点: WEB-INF/web.xml泄露 WEB-INF主要包含一下文件或目录: /WEB-INF/web.xml:Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命 ...

  4. RaspBerry--解决无法用 ssh 直接以 root 用户登录

    参考:https://www.cnblogs.com/xwdreamer/p/6604593.html 以普通用户登录,然后切换至 root 用户. 编辑 /etc/ssh/sshd_config 添 ...

  5. Java线程(一)——创建线程的两种方法

    Thread 和 Runnable Java程序是通过线程执行的,线程在程序中具有独立的执行路径.当多条线程执行时,它们之间的路径可以不同,例如,一条线程可能在执行switch的一个case语句,另一 ...

  6. PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]

    题目 Eva would like to make a string of beads with her favorite colors so she went to a small shop to ...

  7. 寒假day06

    今天完善了毕设的数据抽取功能,新增了几点: 1.已经抽取过的表由系统给出相应提示 2.生成数据抽取记录并展示 3.界面优化

  8. tensorflow C++接口调用目标检测pb模型代码

    #include <iostream> #include "tensorflow/cc/ops/const_op.h" #include "tensorflo ...

  9. ES6之展开运算符

    本文介绍ES6新增的展开运算符(spread operator). 由上图可得,展开运算符负责拼装数组和对象,与之相反,解构赋值负责分解数组和对象. 由上图可得,展开运算符能和解构赋值一起发挥成更大的 ...

  10. Linux Man手册的使用示例

    转载自:https://blog.csdn.net/ac_dao_di/article/details/54718710 Linux的命令非常多,很多人在学一个命令时,首先想到的是使用百度或者谷歌,或 ...