-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_palindrome_number.py
59 lines (53 loc) · 1.37 KB
/
9_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
# 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 = len(str(x))
tmp = x
for i in range(int(ls/2)):
right = int(tmp % 10)
left = tmp / (10 ** (ls - 2 * i - 1))
left = int(left % 10)
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)