🐙
LeetCode0008
9. Palindrome Number
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