125. 验证回文串【isalnum()】

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower()
        # print(s)
        news = ""
        # s[1].is
        for i in range(len(s)):
            if s[i].isalnum(): # 题目要求:只考虑字母和数字字符
                news+=s[i]
        # print(news)
        # news = 'aba'
        length = len(news)
        for i in range(length//2):
            if news[i] != news[length-i-1]:
                return False
        return True

字符串补充:

字符串中str.count(i),计算单个字母出现的次数;

import collections的用法又忘记了ToT;

猜你喜欢

转载自blog.csdn.net/weixin_31866177/article/details/83419325