🐙

LeetCode0008

に公開

9. Palindrome Number

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

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

revでxの半分を保存しておく.数字の桁数では rev > x となるので,x == rev またはx == rev//10 であれば左右対称である.

Discussion