Skip to content

Commit baa5e31

Browse files
Update 009_Palindrome_Number.py (#42)
A trick to make it simple and a lot faster
1 parent 0564594 commit baa5e31

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

python/009_Palindrome_Number.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
# """
77

88
class Solution(object):
9-
def isPalindrome(self, x):
10-
if x < 0:
11-
return False
12-
ls = len(str(x))
13-
tmp = x
14-
for i in range(int(ls/2)):
15-
right = int(tmp % 10)
16-
left = tmp / (10 ** (ls - 2 * i - 1))
17-
left = int(left % 10)
18-
if left != right:
19-
return False
20-
tmp = tmp // 10
21-
return True
9+
def isPalindrome(self, x: int) -> bool:
10+
x = str(x)
11+
if (x == x[::-1]):
12+
return True
13+
return False
14+
15+
16+
# def isPalindrome(self, x):
17+
# if x < 0:
18+
# return False
19+
# ls = len(str(x))
20+
# tmp = x
21+
# for i in range(int(ls/2)):
22+
# right = int(tmp % 10)
23+
# left = tmp / (10 ** (ls - 2 * i - 1))
24+
# left = int(left % 10)
25+
# if left != right:
26+
# return False
27+
# tmp = tmp // 10
28+
# return True
29+
2230

2331
# def isPalindrome(self, x):
2432
# #leetcode book

0 commit comments

Comments
 (0)