9、回文数

9、回文数

难度:简单

题目:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

Python:

初始方案:116 ms

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0 or (x%10 == 0 and x!=0):
            return False
        else:
            temple = x
            y = 0
            while x != 0:
                y = y * 10 + x%10
                x = x//10
            return y == temple

进阶方案:只翻转一半的数 88ms

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0 or (x%10 == 0 and x!=0):
            return False
        else:
            y = 0
            while x > y:
                y = y * 10 + x%10
                x = x//10
            return x == y or x == y//10
posted @ 2020-08-21 00:06  ZeroCrow  阅读(63)  评论(0)    收藏  举报