Skip to content

Commit 3cf937c

Browse files
Add files via upload
1 parent 2eadab6 commit 3cf937c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 第一种思路,将数字转换为字符串
2+
# 252ms 99.32%
3+
class Solution:
4+
def isPalindrome(self, x):
5+
"""
6+
:type x: int
7+
:rtype: bool
8+
"""
9+
strs = str(x)
10+
return True if strs == strs[::-1] else False
11+
12+
# 第二种思路,把数字翻转过来判断两者是否相等
13+
# 268ms 90.11%
14+
class Solution:
15+
def isPalindrome(self, x):
16+
"""
17+
:type x: int
18+
:rtype: bool
19+
"""
20+
if x < 0:
21+
return False
22+
reverse, temp_x = 0, x
23+
while x >= 10:
24+
reverse = reverse * 10 + x % 10
25+
x //= 10
26+
reverse = reverse * 10 + x
27+
return True if reverse == temp_x else False
28+
29+
# 第三种思路,不必翻转所有的数据,只需要翻转一半就可以了
30+
# 260ms 97.20%
31+
class Solution:
32+
def isPalindrome(self, x):
33+
"""
34+
:type x: int
35+
:rtype: bool
36+
"""
37+
if x < 0 or (x % 10 is 0 and x is not 0):
38+
return False
39+
reverse = 0
40+
while x > reverse:
41+
reverse = reverse * 10 + x % 10
42+
x //= 10
43+
return True if reverse == x or reverse // 10 == x else False

0 commit comments

Comments
 (0)