zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 9. Palindrome Number

    #回文数
    #Method1:将整数转置和原数比较,一样就是回文数;负数不是回文数
    #这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑
    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x<0:return False
            #负数不是回文数,return False
            xre=x
            ans=0
            while x>0:
                ans=ans*10+x%10
                x=x//10
            
            if ans>21474836547:
                ans=0
            print ans ,xre
            return ans==xre


    #Method2:不反转整数,将数字逐个分离,比较最前与最后是否一样
    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x<0:return False
            #负数不是回文数,return False
            digits=1
            while x/digits>=10:
                digits*=10
            
            while digits>1:
                right=x%10
                left=x/digits
                if left!=right:return False
                x=(x%digits)/10
                digits/=100
            
            return True

  • 相关阅读:
    《javascript设计模式》2接口
    对css类名className的一些操作的函数
    js设计模式方法的链式调用及回调
    js设计模式封装
    ajax的封装
    js设计模式单体(Singleton)
    js设计模式继承
    metasploit
    使用 AsyncCallback 处理异步调用
    log4net 的使用
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5955514.html
Copyright © 2011-2022 走看看