LeetCode - Palindromic Number

Determines whether an integer is a palindrome. A palindrome is an integer that reads the same in positive order (from left to right) and in reverse order (from right to left).

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        string1 = str(x)
        list1 = list(string1)
        list2 = list(string1)
        list1.reverse()
        if list1 == list2:
            return True
        else:
            return False

Because there is a reverse method in the list, after converting the number into a list, flip the list, and the comparison concludes that the
list.reverse() method is to reverse the list in the memory address and does not return a new list, so the address of list1 cannot be saved in list2. Instead, assign the memory address of list(string1) to list2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568729&siteId=291194637