-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_number.py
64 lines (51 loc) · 1.32 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
56
57
58
59
60
61
62
63
64
class Solution:
""" Naive make int a string """
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
string = str(x)
length = len(string)
is_even = length%2
if length == 1:
return True
half = length//2
return string[:half] == string[half+is_even:][::-1]
def isPalindrome_string(self, x):
x = str(x)
return x[0::]==x[::-1]
class Solution:
""" No extra space, no integer overflow """
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
div = 1
while x//div >= 10:
div *= 10
while x != 0:
l = x // div
r = x % 10
if l != r:
return False
x = (x % div) // 10
div //= 100
return True
class Solution:
""" No extra space, but possible integer overflow """
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
copy, reverse = x, 0
while copy:
reverse *= 10
reverse += copy % 10
copy //= 10
print(reverse)
return x == reverse