class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ if x < 0: return False while x > 9 : len_x = self.digits(x) tail = x%10 head = x/(10**( len_x -1 ) ) if tail!=head: return False else: # cut head and tail from x x = x - head*( 10**(len_x - 1) ) - tail shrink = len_x - self.digits(x) # cut tail from x and deal with cases of 0's following head cnt = 0 #print head, tail, x, shrink while cnt < shrink: if x%10 != 0: return False x = x/10 cnt += 1 return True def digits(self , x): cnt = 0 while x > 0: x=x/10 cnt += 1 return cnt test = Solution() print test.isPalindrome(100001)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question