Skip to content

Commit 36cd4d8

Browse files
author
shangchun
committedDec 5, 2014
leetcode
1 parent d4c0e94 commit 36cd4d8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎palindromeNumber.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# @return an boolean
2+
def isPalindrome(x):
3+
if x < 0:
4+
return False
5+
if x < 10:
6+
return True
7+
8+
# count digits
9+
count = 0
10+
xx = x
11+
while xx > 0:
12+
xx = xx / 10
13+
count += 1
14+
15+
i = 0
16+
while i < count - 1:
17+
if ((x % (10 ** (i + 1))) / (10 ** i)) != ((x % (10 ** count)) / (10 ** (count - 1))):
18+
return False
19+
i += 1
20+
count -= 1
21+
return True
22+
23+
24+
assert isPalindrome(121) == True
25+
assert isPalindrome(12321) == True
26+
assert isPalindrome(123) == False
27+
assert isPalindrome(1) == True
28+
assert isPalindrome(-1) == False

0 commit comments

Comments
 (0)