当前位置 : 主页 > 手机开发 > ROM >

Palindrome Number

来源:互联网 收集:自由互联 发布时间:2021-06-10
Determine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward.Coud you solveit without converting the integer to a string? my solution def is_palindrome(x): """ :type x: int :rtype: bool """

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

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

reference

https://leetcode.com/problems/palindrome-number/submissions/

网友评论