-
Notifications
You must be signed in to change notification settings - Fork 43
/
palindrome-number.py
55 lines (50 loc) · 1.38 KB
/
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
# V0
class Solution(object):
# @return a boolean
def isPalindrome(self, x):
length = len(str(x))
if length==1 or length==0:
return True
if x < 0:
return False
mid = length//2
return str(x)[:mid] == str(x)[-mid:][::-1]
### test case
s = Solution()
assert s.isPalindrome(100) == False
assert s.isPalindrome(3456) == False
assert s.isPalindrome(1001) == True
assert s.isPalindrome(1) == True
#assert s.isPalindrome(None) == True
assert s.isPalindrome(10**32 + 1) == True
assert s.isPalindrome(10**32 + 3) == False
# V1
import math
class Solution(object):
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False
x = str(x)
# if x is like 12321 (odd length)
if str(x)[:math.floor(len(str(x))/2)] == x[math.floor(len(str(x))/2)+1:][::-1]:
return True
# if x is like 2222 (even length)
if str(x)[:math.floor(len(str(x))/2)] == x[math.floor(len(str(x))/2):][::-1]:
return True
else:
return False
# V2
# Time: O(1)
# Space: O(1)
class Solution(object):
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False
copy, reverse = x, 0
while copy:
reverse *= 10
reverse += copy % 10
copy //= 10
return x == reverse