Stay button (LeetCode 9) palindrome python

topic

Determine whether an integer is a palindrome. Palindrome correct order (from left to right) and reverse (right to left) reading is the same integer.

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
explanation: read from left to right, as -121. Reading from right to left, as 121-. So it is not a palindrome number.
Example 3:

Input: 10
Output: false
interpretation: reading from right to left, to 01. So it is not a palindrome number.

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/palindrome-number
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Thinking

Negative definitely not a palindrome, flip a positive number and the original number compared to the same palindrome

Code

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x<0:
            return False
        
        ans=0
        num=x
        while(num):
            mod=num%10
            num=num//10
            ans=ans*10+mod
        
        if x==ans:
            return True
        return False
        

Guess you like

Origin blog.csdn.net/qq_41318002/article/details/93750637