Palindrome number python

Author: CYL

Date: 2020-9-29

Tags: simple numeric string

Title description

Determine whether an integer is a palindrome. The palindrome number refers to the same integer in both positive order (from left to right) and reverse order (from right to left).

Example

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
Explanation: Read from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reading from right to left, it is 01. Therefore it is not a palindrome.

Problem solving ideas

Use string reverse order to compare whether the reverse order is equal to the one before the reverse order

Code

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x<0 :
            return False
        else:
            x_str = str(x)
            x_rev = x_str[::-1]
            if x_str == x_rev:
                return True
            else:
                return False

Guess you like

Origin blog.csdn.net/cyl_csdn_1/article/details/108874775