-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPalindromeNumber.py
More file actions
32 lines (26 loc) · 804 Bytes
/
PalindromeNumber.py
File metadata and controls
32 lines (26 loc) · 804 Bytes
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
'''
Determine whether an integer is a palindrome. Do this without extra space.
'''
import math
class Solution(object):
def isPalindrome(self, x):
if x < 0: # negative numbers
return False
if x == 0:
return True
numberOfDigits = int(math.log(x, 10))
divisor = 10 ** numberOfDigits
while x:
l, r = x // divisor, x % 10
if l != r:
return False
x = (x % divisor) / 10
divisor //= 100
return True
if __name__ == "__main__":
s = Solution()
assert True == s.isPalindrome(1)
assert False == s.isPalindrome(-1221)
assert False == s.isPalindrome(12211)
assert True == s.isPalindrome(1221)
assert True == s.isPalindrome(12321)