-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeNumber.py
More file actions
41 lines (32 loc) · 1.06 KB
/
PalindromeNumber.py
File metadata and controls
41 lines (32 loc) · 1.06 KB
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
# Question link - https://leetcode.com/problems/palindrome-number/description/?envType=study-plan-v2&envId=top-interview-150
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
# Sol2: Inplace palindrome with O(x)
# Negative
if x < 0: return False
# Dividian - as div significant value for the left digit
div = 1
while x >= 10 * div:
div *= 10
while x:
right = x % 10
left = x // div
if left != right:
return False
# Chop up tha digits Right - > (x % div) , left -> (x % div) // 10
x = (x % div) // 10
# Reduce the div upto 2 digit - > // 100
div = div // 100
return True
# Sol : This is the traditional solution ,with huge complexity
# temp = x
# rev = 0
# while temp:
# r = temp % 10
# rev = rev * 10 + r
# temp = temp / 10
# return rev == x