9 check Palindrome Number by using python

PPZ發表於2015-02-14

By splitting string python can deal with this problem easily.

string[a:b:c]

意思是從c為間隔從a開始一直取到b(注意不含b) s[0:8] returns everything up to, but not including, the character at position 8

so reverse a string a can be a[::-1]

class Solution:
# @return a boolean
def isPalindrome(self, x):
    a=str(x)
    b=a[::-1]
    if a==b:
        return True
    else:
        return False

Before I tried to use the common method like I did in C, using for loop to give x and reversion of x to different string a,b and check if a and b are same. But I found it has a mistake. Because the string in python is immutable. I CAN'T DO a[i]=x[i].

相關文章