[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 ...
随机推荐
- 从零开始入门 K8s | Kubernetes 存储架构及插件使用
本文整理自<CNCF x Alibaba 云原生技术公开课>第 21 讲. 导读:容器存储是 Kubernetes 系统中提供数据持久化的基础组件,是实现有状态服务的重要保证.Kubern ...
- (综合)P2089 烤鸡
题解: 错误的: #include<stdio.h>int n,ret=0,a[10000][10];int p(int c,int s){ int i; for(i=1;i<=3; ...
- 洛谷P1002 过河卒(动态规划)
题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为 ...
- VUE,index key v-for
列表渲染语法 v-forv-for 循环对象 <article v-for="(item, key, index) of info">{{item}} {{key}} ...
- Django2.0——请求与响应(上)
客户端与服务段通过http协议进行数据的传输,而http协议是一种双向单工的,且主动发起连接的只有客户端.故数据的传送就离不开请求和响应,客户端每发起一个请求,服务端就是返回一个响应.在django的 ...
- h5-其他伪元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 爬虫防止浏览器防止debug处理
方式一(基于你会前端,我比较喜欢这种方式) #复制html页面 #复制其中的js,css(css可有可无,如果加css和不加css情况不一样,网页可能做了css反爬处理) #全局搜索debug or ...
- JAVA多线程之状态转换图
线程状态转换图如下: 1.新建(new):线程对象被创建后就进入了新建状态.如:Thread thread = new Thread(); 2.就绪状态(Runnable):也被称为“可执行状态”.线 ...
- sklearn连续型数据离散化
二值化 设置一个condition,把连续型的数据分类两类.比如Age,大于30,和小于30. from sklearn.preprocessing import Binerize as Ber x ...
- Java 实现 栈
package Test; import java.util.*; public class Stack_test { public static void main(String[] args) { ...