zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):125 Valid Palindrome

    题目来源


    https://leetcode.com/problems/valid-palindrome/

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

    For example,
    "A man, a plan, a canal: Panama" is a palindrome.
    "race a car" is not a palindrome.

    Note:
    Have you consider that the string might be empty? This is a good question to ask during an interview.

    For the purpose of this problem, we define empty string as valid palindrome.


    题意分析


    Input: a string

    Output: whether the string is valid palindrome

    Conditions: 判断是否是回文串,忽略非数字和非字母的字符


    题目思路


    采用isalnum来判断是否为字母或数字,然后将字符转为小写(题目认为大小写是一样的),然后简单判断是否是回文。


    AC代码(Python)

     1 class Solution(object):
     2     def isPalindrome(self, s):
     3         """
     4         :type s: str
     5         :rtype: bool
     6         """
     7         c = []
     8         for i in s:
     9             if i.isalnum():
    10                 c.append(i.lower())
    11         for i in range(len(c)/2):
    12             if c[i] != c[len(c)-1-i]:
    13                 return False
    14         return True
  • 相关阅读:
    [hdu 2089] 不要62 数位dp|dfs 入门
    [poj 2456] Aggressive cows 二分
    NOIP 2016 DAY 1 爆炸记
    【模板】kmp
    【模板】LCA
    NOIP 之前的吐槽
    【模板】树状数组
    【模板】线段树
    【模板】高精度
    2016/11/15
  • 原文地址:https://www.cnblogs.com/loadofleaf/p/5524001.html
Copyright © 2011-2022 走看看