zoukankan      html  css  js  c++  java
  • LeetCode_9_回文数字

    回文数(LeetCode 9)

    1.题目

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

    示例 1:

    输入: 121
    输出: true
    

    示例 2:

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

    示例 3:

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

    进阶:

    你能不将整数转为字符串来解决这个问题吗?

    2.分析

    整数逆置

    3.代码

    不用自己写反转

        def isPalindrome(self, x: 'int') -> 'bool':
            x = str(x)
            new_x = x[::-1]
            if new_x == x: 
                    return True
            return False
    

    自己写反转判断

        def isPalindrome(self, x: 'int') -> 'bool':
            x = str(x)
            return self.func(x)
        def func(self, x):
            l, r =0, len(x)-1
            while l<r:
                if x[l] != x[r]:
                    return False
                l += 1
                r -= 1
            return True
    

    整数逆置

        def isPalindrome(self, x: 'int') -> 'bool':
            # 如果负数,不是回文数;如果个位数是0(除0这种特殊情况),不是回文数
            if x<0 or (x!=0 and x%10==0):
                return False
            y = x
            n = 0
            # 逆置 整数
            while x:
                n = n * 10 + x % 10
                x = x//10
            return n==y
    

    反转一半数

       # 反转一半数
        def isPalindrome(self, x: 'int') -> 'bool':
            if x<0 or (x!=0 and x%10==0):
                return False
            right_rev = 0
            while x > right_rev:
                right_rev = right_rev*10 + x%10
                x = x//10
           #    奇偶情况都考虑
            return x==right_rev or x==right_rev//10
  • 相关阅读:
    VS.net 2005快捷键一览表
    POJ 1141 Brackets Sequence
    POJ 3264 Balanced Lineup RMQ问题的ST解法
    Hdu 4267 A Simple Problem with Integers
    hdu 4009 Transfer water
    HDU 4288 Coder
    POJ 1679 The Unique MST
    hdu 4291 A Short problem
    hdu 1175 连连看(DFS)
    POJ 3164 Command Network
  • 原文地址:https://www.cnblogs.com/biggw/p/11334502.html
Copyright © 2011-2022 走看看