125. Valid Palindrome

来源:互联网 发布:淘宝客挣钱 编辑:程序博客网 时间:2024/06/10 12:39

125. Valid Palindrome

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

For example,

"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome

今天看SVM看的很沮丧。。对自己下一步该怎么走也十分忧愁,毕竟校招已经开始了,各大招聘如火如荼,自己却在这个时候没有着急面试,而是选择转换方向,心里不急是安慰罢了,但还是希望自己能够镇定,不要急不要慌,该来的总会来的。

 

这里相当于用了两个指针吧,一个从前往后j,一个从后往前end,遍历过程中一旦有不一样的情况就返回False,遍历截止条件是到达中间(这里用//除法,不管总位数是奇数还是偶数,都只取两两对半,不考虑最中间那个数)


class Solution(object):    def isPalindrome(self, s):        """        :type s: str        :rtype: bool        """        if s=='':            return True        s= ''.join([i for i in s if i.isalnum()])  # isalnum()  isalpha()        s=s.lower()        half = (len(s))//2        j=0        end=-1        while j<half:            if s[j] != s[end]:                return False            j+=1            end-=1        return True

原创粉丝点击