Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Coud you solve it without converting the integer to a string?
my solution
思路:
1)不断取余将所有位数的数字添加到列表里
2)将列表倒序并与原列表比较
def is_palindrome(x): """ :type x: int :rtype: bool """ if x >= 0: num_list = [] while x > 0: a = x % 10 x = x // 10 num_list.append(a) rev_num_list = num_list[::-1] return rev_num_list == num_list return False
other solution
思路:
1)一边取余一边将除数与余数组合成一个新数字
2)将新数字与原数字比较
def is_palindrome(x): if x >= 0: p, res = x, 0 while p: res = res*10 + p % 10 p //= 10 return res == x return False
reference