forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path009_Palindrome_Number.py
65 lines (58 loc) · 1.46 KB
/
009_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# class Solution(object):
# def isPalindrome(self, x):
# """
# :type x: int
# :rtype: bool
# """
class Solution(object):
def isPalindrome(self, x):
if x < 0:
return False
ls = 0
tmp = x
while tmp != 0:
ls += 1
tmp = tmp // 10
tmp = x
for i in range(ls/2):
right = tmp % 10
left = tmp / (10 ** (ls - 2 * i - 1))
left = left % 10
# print left, right
if left != right:
return False
tmp = tmp // 10
return True
# def isPalindrome(self, x):
# #leetcode book
# if x < 0:
# return False
# div = 1
# while x / div >= 10:
# div *= 10
# while x != 0:
# left = x / div
# right = x % 10
# if left != right:
# return False
# x = (x % div) / 10
# div /= 100
# return True
# def isPalindrome(self, x):
# # reverse number
# if x < 0:
# return False
# rev_x = 0
# temp = x
# while temp != 0:
# curr = temp % 10
# rev_x = rev_x * 10 + curr
# temp = temp // 10
# if rev_x == x:
# return True
# else:
# return False
if __name__ == '__main__':
# begin
s = Solution()
print s.isPalindrome(1001)