-
Notifications
You must be signed in to change notification settings - Fork 0
/
nine_palindrome_number.py
59 lines (51 loc) · 1.69 KB
/
nine_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
# Determine whether an integer is a palindrome. Do this without extra space.
def is_palindrome(num):
"""
:type num: int
:rtype: bool
"""
if num < 0:
return False
if num < 10:
return True
if num < 100:
right = num/10
left = num%10
return right == left
pivot = 10
left = -1
right = -1
pivot_time = 1
num_length = 0
while right != 0:
right = num/pivot
left = num%pivot
print('pivot: {pivot}, right:{right}, left:{left}'.format(pivot=pivot, right=right, left=left))
if pivot > right:
if right >= pivot/10:
num_length = pivot_time * 2
pivot /= 10
for time in range(1,pivot_time+1):
print('pivot: {pivot}, right:{right}, left:{left}'.format(pivot=pivot, right=right/pivot, left=left% 10))
if right/pivot != left% 10:
return False
right %= pivot
left /= 10
pivot /= 10
else:
num_length = pivot_time * 2 -1
pivot /= 10
left %= pivot
pivot /= 10
for time in range(1,pivot_time):
print('pivot: {pivot}, right:{right}, left:{left}'.format(pivot=pivot, right=right/pivot, left=left% 10))
if right/pivot != left% 10:
return False
right %= pivot
left /= 10
pivot /= 10
print(num_length)
return True
pivot *= 10
pivot_time += 1
print(is_palindrome(123321))