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. 小白需要了解的Ajax和websocket的区别以及使用场景!

    在我们日常使用的互联网产品中,很多都是前后端数据的交互来完成的,说到数据交互就不得不提Ajax和websocket,它们可是数据交互的利器,那么它们分别是什么?websocket与Ajax轮询的区别又 ...

  2. UML-用例关联

    1.用例关联:就是各个用例之间的关系,分3种关系分别是:包含关系.扩展关系.泛化关系. 2.包含关系 1).示例 2).使用场景 A.用例在其他用例中重复使用 B.用例非常复杂冗长,将其分解为子单元便 ...

  3. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  4. tomcat用户配置

    1.tomcat-user.xml 添加以下内容<role rolename="manager-gui"/> <role rolename="manag ...

  5. 洛谷P1257(暴力超时)

    1.先输入再求勾股定理会超时 2.需要一边输入一边求. #include<iostream> #include<cmath>#include<cstdio> usi ...

  6. ZJNU 2351 - 快乐

    由题意得,如果有个人从前往后能找到第一个不低于自己等级的任务,就会接取其后所有任务 那么就可以让输入数据处理成递增数列 例如1 3 5 4 6 2 7 7 3 可以处理成1 3 5 5 6 6 7 7 ...

  7. PAT Basic 1070 结绳(25) [排序,贪⼼]

    题目 给定⼀段⼀段的绳⼦,你需要把它们串成⼀条绳.每次串连的时候,是把两段绳⼦对折,再如下图所示套接在⼀起.这样得到的绳⼦⼜被当成是另⼀段绳⼦,可以再次对折去跟另⼀段绳⼦串连.每次串 连后,原来两段绳 ...

  8. java 利用管道实现线程间通信

    package com.lb; import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutpu ...

  9. tensorflow C++接口调用图像分类pb模型代码

    #include <fstream> #include <utility> #include <Eigen/Core> #include <Eigen/Den ...

  10. CCPC2019网络赛

    2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A 题意:找到最小的正整数 C 使得 (A^C)&(B^C) 最小. \(A,B \le 10^9\) 签到题.这个C取 A& ...