作者: 负雪明烛
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. 巩固javaweb第十四天

    巩固内容: 单行文本框: 单行文本框的基本语法格式如下: < input type="text"  name="输入信息的字"  value=" ...

  2. day06 视图层

    day06 视图层 今日内容 视图层 小白必会三板斧 JsonResponse form表单发送文件 FBV与CBV FBV基于函数的视图 CBV基于类的视图 模板层 模板语法的传值 模板语法之过滤器 ...

  3. [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现

    漏洞影响范围 Yii2 < 2.0.38 环境搭建 Yii2.0.37 漏洞分析 首先定位到漏洞起始点 为什么是这儿?我们该怎么发现是某个类的某个函数?为什么不是其他函数? 一般是__destr ...

  4. 【DFS与BFS】洛谷 P1135 奇怪的电梯

    题目:奇怪的电梯 - 洛谷 (luogu.com.cn) 因为此题数据范围较小,有dfs及bfs等多种做法. DFS 比较正常的dfs,注意vis数组一定要回溯,不然会漏情况 例如这个数据 11 1 ...

  5. OpenStack之五: image镜像服务(端口9292)

    官网地址:https://docs.openstack.org/glance/stein/install/install-rdo.html #:创建glance库,并授权 MariaDB [(none ...

  6. mysql死锁com.mysql.cj.jdbc.exception.MYSQLTransactionRollbackException Deadlock found when trying to get lock;try restarting transaction

    1.生产环境出现以下报错 该错误发生在update操作中,该表并未建立索引,也就是只有InnoDB默认的主键索引,发生错误的程序是for循环中update. 什么情况下会出现Deadlock foun ...

  7. 最小化安装centos ubuntu基础命令

    # yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \ pcre-devel openssl openssl-devel zip ...

  8. 使用jsp制作index,可以通过<c:if test==“管理员”>或<c:if test=="客户">来区别展示用户界面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ ta ...

  9. java输入/输出流的基本知识

    通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...

  10. 全网最详细的AbstractQueuedSynchronizer(AQS)源码剖析(一)AQS基础

    AbstractQueuedSynchronizer(以下简称AQS)的内容确实有点多,博主考虑再三,还是决定把它拆成三期.原因有三,一是放入同一篇博客势必影响阅读体验,而是为了表达对这个伟大基础并发 ...