Python3 determine whether the palindrome

Python3 determine whether the palindrome

Original title https://leetcode-cn.com/problems/palindrome-number/

Title:
do not allow the method str!
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:

输入: 121
输出: true

Example 2:

输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

Example 3:

输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。

Problem solving:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False
        old_x = x
        rev = 0
        while x != 0:
            pop = x % 10
            x = int(x / 10)
            rev = rev * 10 + pop
        return rev == old_x
Published 24 original articles · won praise 0 · Views 411

Guess you like

Origin blog.csdn.net/qq_18138105/article/details/105171159