#-*- 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)

【leetcode❤python】Convert a Number to Hexadecimal的更多相关文章

  1. 【leetcode❤python】 374. Guess Number Higher or Lower

    #-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...

  2. 【leetcode❤python】 9. Palindrome Number

    #回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object):  ...

  3. 【leetcode❤python】263. Ugly Number

    class Solution(object):    def isUgly(self, num):        if num<=0:return False        comlist=[2 ...

  4. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  5. 【leetcode❤python】 414. Third Maximum Number

    #-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...

  6. 【leetcode❤python】191. Number of 1 Bits

    #-*- coding: UTF-8 -*- class Solution(object):    def hammingWeight(self, n):        if n<=0:retu ...

  7. 【leetcode❤python】171. Excel Sheet Column Number

    #-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string. ...

  8. 【LeetCode OJ】Convert Sorted List to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...

  9. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

随机推荐

  1. Script to set the Purchase Order Status to ‘OPEN’(将采购订单重新打开)

    Business Requirement: The finance user requests the IT team to change the PO status to OPEN as they ...

  2. 夺命雷公狗—express—1—express的配置方法和目录结构分析

  3. 夺命雷公狗---2016-linux---2之软件实现远程登录

    废话不多说,操作方法如下所示:

  4. [记录]使用openGL显示点云的一个程序

    #include <GL/glut.h> #include <stdio.h> #include <iostream> using namespace std; v ...

  5. [PHP100]留言板(一)

    [实例]我的留言板 ** 文件结构: conn.php // 数据库配置 add.php // 操作文件 list.php //列表文件 ** 步骤 建立数据库: phpmyadmin: 建立数据库( ...

  6. 如何对HashMap按键值排序

    Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...

  7. Ul li 横排 菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. JS调用Java函数--DWR框架

    (1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJA ...

  9. zabbix监控nginx

     nginx status详解 active connections – 活跃的连接数量server accepts handled requests — 总共处理了11989个连接 , 成功创建11 ...

  10. Web Token JWT

    基于Token的WEB后台认证机制 JSON Web Token in ASP.NET Web API 2 using Owin 翻译:Token Authentication in ASP.NET ...