python—判断回文数
程序员文章站
2022-05-09 18:01:47
题目:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。方法一:执行用时: 112 ms内存消耗: 13.3 MBclass Solution: def isPalindrome(self, x: int) -> bool: num = 0 a = x while a > 0: b = a % 10 a = a // 10...
题目:
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
方法一:
执行用时: 112 ms
内存消耗: 13.3 MB
class Solution:
def isPalindrome(self, x: int) -> bool:
num = 0
a = x
while a > 0:
b = a % 10
a = a // 10
num = num*10 + b
if num == x:
return True
else:
return False
方法二:
执行用时: 84 ms
内存消耗: 13.5 MB
class Solution:
def isPalindrome(self, x: int) -> bool:
str_num = str(x)
for i in range(len(str_num)):
if str_num[i] != str_num[-(i+1)]:
return False
return True
本文地址:https://blog.csdn.net/weixin_45455015/article/details/109959998