作者: 负雪明烛
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++)的更多相关文章

  1. Java [Leetcode 125]Valid Palindrome

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

  2. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  3. [LeetCode] 125. Valid Palindrome 验证回文字符串

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

  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. [leetcode]125. Valid Palindrome判断回文串

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

  7. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  8. Java for LeetCode 125 Valid Palindrome

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

  9. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

随机推荐

  1. 8核cpu,,负载

    今天有一个电话面试,面试官问我:CentOS怎么查看CPU负载?我说:看top的第一行有load average.面试官又问:为什么从这就判定是负载高呢?依据是什么呢?然后... 然后我就尴尬了,挂了 ...

  2. adverb

    An adverb is a word or an expression that modifies a verb, adjective, another adverb, determiner [限定 ...

  3. 商业爬虫学习笔记day7-------解析方法之bs4

    一.Beautiful Soup 1.简介 Beautiful Soup 是python的一个库,最主要的功能是从网页抓取数据.其特点如下(这三个特点正是bs强大的原因,来自官方手册) a. Beau ...

  4. linux添加用户、权限

    # useradd –d /usr/sam -m sam 此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户主目录所在的父目录). 假 ...

  5. ps精修

    1.磨皮方法: a,, 添加高斯模糊后,按住alt键新建图层蒙版,设置前景色为白色,用画笔在脸上雀斑的位置涂抹,注意脸轮廓位置不要涂抹.最后添加曲线提亮 b. 添加蒙尘和划痕后,后面上面的一样

  6. Template Metaprogramming in C++

    说实话,学习C++以来,第一次听说"Metaprogramming"这个名词. Predict the output of following C++ program. 1 #in ...

  7. Linux基础命令----smbclient

    smbclient smbclient是一个smb服务器的客户端的管理程序,可以交互式的访问samba服务器. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.SU ...

  8. 设置linux下oracle开机自启动

    1.修改配置文件,vi /etc/oratab orcl:/u01/app/oracle/product/11.2.0/db_1:Y 2.创建启动文件,/etc/init.d/ #!/bin/sh # ...

  9. Apache Log4j 2 报高危漏洞,CODING 联手腾讯安全护卫软件安全

    导语 12 月 9 日晚间,Apache Log4j 2 发现了远程代码执行漏洞,恶意使用者可以通过该漏洞在目标服务器上执行任意代码,危害极大. 腾讯安全第一时间将该漏洞收录至腾讯安全漏洞特征库中,C ...

  10. idea 无法创建子目录

    idea 无法创建子目录 解决方案