-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_number.py
42 lines (35 loc) · 1005 Bytes
/
palindrome_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# encoding: utf8
# Determine whether an integer is a palindrome. Do this without extra space.
__author__ = "Zhu Xianfeng <[email protected]>"
class Solution:
def _isPalindrome(self, num, size):
if size < 2:
return True
# size >= 2
head = (num / (10 ** (size - 1))) % 10
tail = num % 10
# print "num = %d, size = %d, head = %d, tail = %d" % (num, size, head, tail)
if head == tail:
num %= (10 ** (size - 1))
num /= 10
return self._isPalindrome(num, size - 2)
else:
return False
# @return a boolean
def isPalindrome(self, x):
# x = abs(x)
if x < 0:
return False
n = x
i = 1
while n >= 10:
n /= 10
i += 1
return self._isPalindrome(x, i)
def main():
num = -2147447412
sol = Solution()
print sol.isPalindrome(num)
if __name__ == "__main__":
main()