zoukankan      html  css  js  c++  java
  • 【leetcode❤python】Convert a Number to Hexadecimal

    #-*- coding: UTF-8 -*-
    class Solution(object):
        hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',
                10:'a',
                11:'b',
                12:'c',
                13:'d',
                14:'e',
                15:'f'}
        
        
        def toHex(self, num):
            returnStr=[]
            if(num==0):
                return '0'
            if num>=pow(2,32) or num <-pow(2,32):
                return False
            if(num<0):
                print num
                num=pow(2,32)-1-abs(num)+1
                print num
         
            while True:
                remainder=num%16
                divisior=num/16

                returnStr.append(self.hexDic.get(remainder))
                print remainder,divisior
                if(divisior<16):
                    returnStr.append(self.hexDic.get(divisior))
                    break
                num=divisior
            print returnStr
            returnStr.reverse()
            if returnStr[0]=='0':del returnStr[0]
            return ''.join(returnStr)

    sol=Solution()
    print sol.toHex(1)

  • 相关阅读:
    01:求平均年龄
    09:与圆相关的计算
    08:温度表达转化
    07:计算多项式的值
    06:甲流疫情死亡率
    05:计算分数的浮点数值
    04:带余除法
    03:计算(a+b)/c的值
    02:计算(a+b)*c的值
    01:A+B问题
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5953688.html
Copyright © 2011-2022 走看看