【LeetCode】125. Valid Palindrome 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/valid-palindrome/description/
题目描述
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
解题方法
列表生成式
python 处理起来很简单。isalnum()能判断字符是不是字母数字的,用列表表达式就能产生有效字符串,然后就可以看是不是回文。
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
isValid = lambda x : x == x[::-1]
string = ''.join([x for x in s.lower() if x.isalnum()])
return isValid(string)
正则表达式
二刷的时候使用的正则表达式。然后写了一个判断是不是回文的循环,速度很快。
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
s = s.lower()
s = re.sub("\W", "", s)
N = len(s)
left, right = 0, N - 1
while left <= right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
双指针
使用双指针只需要一次遍历即可,如果遇到不是字母或者数字的字符,直接继续向中间走;
如果左右指针都是字母或者数字的话,那么进行判断是否相等,因为需要忽略掉大小写,所以需要统一处理大小写字母的情况。因为小写字母比其对应的大写字母的ASCII码大32,所以如果遇到了大写字母,我们需要先加上32,然后再减去’a’,就知道其相对于’a’的位置了,这个值肯定是小于32的,所以对32取余没啥影响。
如果遇到小写字母,虽然加上了32,但是最后对32取余了,多加的32也就没了,所以还是能得到其相对于’a’的正确位置。
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.size();
while (left <= right) {
if (!isalnum(s[left])) ++left;
else if (!isalnum(s[right])) --right;
else if ((s[left] + 32 - 'a') % 32 != (s[right] + 32 - 'a') % 32) return false;
else {
++left;
--right;
}
}
return true;
}
};
日期
2018 年 2 月 4 日
2018 年 11 月 27 日 —— 最近的雾霾太可怕
【LeetCode】125. Valid Palindrome 解题报告(Python & C++)的更多相关文章
- Java [Leetcode 125]Valid Palindrome
题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 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 ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- Java for LeetCode 125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
随机推荐
- micropython1.16官方文档转PDF
折腾了一天,终于把micropython1.16的官方文档给转成了pdf格式. 不过转换成PDF格式以后存在两点问题: 1.PDF文档有些地方的排版中有些行距没有调整好: 2.使用latex编译tex ...
- 深入浅出KMP
前言:曾经有次在阿里的面试中遇到这个基础的问题,当时知道有这么回事,可是时间久了便 想不起来,可能是不怎么用到,基本调用库什么的,还有个是理解不深刻,不能得到show me the code 的程度, ...
- day02 Linux基础
day02 Linux基础 1.什么是服务器 服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因 此一般来说服务器应具备承担服务并且保障服务的能力. windows: ...
- 19. awk 命令详解
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...
- C语言time函数获取当前时间
以前放了个链接,但是原作者把博文删了,这里放一个获取时间的代码,已经比较详细所以不做注释 #include<stdio.h> #include<time.h> #include ...
- Oracle——概要文件profile
profile文件详解 一.目的 Oracle系统中的profile可以用来对用户所能使用的数据库资源进行限制,使用Create Profile命令创建一个Profile,用它来实现对 ...
- Java实现单链表的增删查改及逆置打印
//所提供的接口 LinkList.java package Struct; public interface LinkList {//判断链表为空public boolean linkListIsE ...
- 查看linux系统CPU和内存命令
cat /proc/cpuinfo查看linux系统的CPU型号.类型以及大小,如下图所示. 通过greap命令根据Physical Processor ID筛选出多核CPU的信息. cat ...
- 多线程异步操作导致异步线程获取不到主线程的request信息
org.springframework.web.context.request.RequestContextHolderorg.springframework.web.context.request. ...
- Vue中如何书写js来渲染页面填充数据的部分代码
new Vue({ el:"#app" , data:{ user:{ id:"", username:"", password:" ...