LeetCode 125. Valid Palindrome

简介: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

v2-4fd1760c6aacb581d6995cb6f332b376_1440w.jpg

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


描述



给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。


说明:本题中,我们将空字符串定义为有效的回文串。


示例 1:

输入: "A man, a plan, a canal: Panama"

输出: true


示例 2:

输入: "race a car"

输出: false


思路



  • 回文字符串是指字符串呈中心对成,即第一个字符与最后一个字符相等,第二个字符与倒数第二个字符相等.
  • 我们用两个指针,一共从前往后遍历,一个从后往前遍历即可,需要注意的是我们只比较字母和数字,其他情况直接跳过.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-03 11:16:37
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-03 11:34:21
class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if not s:
            return True
        left, right = 0, len(s)-1
        while left < right:
            # 如果当前字符是数字或者字母
            if (s[left].isalpha() or s[left].isdigit()) and (s[right].isalpha() or s[right].isdigit()):
                # 如果相等则left向后走一步,right向前走一步
                if s[left].lower() == s[right].lower():
                    left += 1
                    right -= 1
                else:
                    # 否则返回False
                    return False
            # 如果左边不是数字或者字符,则认为是满足回文符串的条件
            elif not (s[left].isalpha() or s[left].isdigit()):
                left += 1
            # 如果右边不是数字或者字符,则认为是满足回文字符串的条件
            elif not (s[right].isalpha() or s[right].isdigit()):
                right -= 1
        return True
if __name__ == "__main__":
    so = Solution()
    res = so.isPalindrome("0P")


源代码文件在这里.


目录
相关文章
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
54 0
LeetCode 409. Longest Palindrome
LeetCode 367. Valid Perfect Square
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
62 0
LeetCode 367. Valid Perfect Square
|
索引
LeetCode 336. Palindrome Pairs
给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。
95 0
LeetCode 336. Palindrome Pairs
LeetCode 242. Valid Anagram
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。
54 0
LeetCode 242. Valid Anagram
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
59 0
LeetCode 214. Shortest Palindrome
|
算法
LeetCode 65. Valid Number
验证给定字符串是否可以解释为十进制数。
66 0
LeetCode 65. Valid Number
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 234. Palindrome Linked List
45 0
Leetcode-Easy 234. Palindrome Linked List
Leetcode-Easy 20. Valid Parentheses
Leetcode-Easy 20. Valid Parentheses
90 0
Leetcode-Easy 20. Valid Parentheses
【LeetCode】Palindrome Pairs(336)
  Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a   palindrome.
81 0

热门文章

最新文章