#-*- 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. sql查询所有表以及表名的模糊查询

    --1.查看所有表名:select name from sysobjects where type='U'--2.查找包含用户的表名,可通过以下SQL语句实现, Select * From sysob ...

  2. 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]

    前一段时间,讲了一系列开发经理.实施经理.服务经理的工具箱:开发经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) ,实施经理的工具箱--走出软件作坊:三五个人十来条枪 如何成 ...

  3. 操作系统,windows编程,网络,socket

    首发:个人博客,更新&纠错&回复 之前关于c/s的一篇博文只记了思路没记代码,而且表达不清晰,事后看不知所云,这个习惯要改. 这十几天学了点关于操作系统.windows编程和网络,主要 ...

  4. expandlistview

    package com.exaple.zhonghe2; import java.sql.SQLData;import java.util.ArrayList;import java.util.Has ...

  5. 分享总结:更好地CodeReview

            代码质量分享    2016_06_24_舒琴_代码质量.key    For 代码提交人     基本原则 Review时机: 对于普通bugfix或优化,CodeReview最迟要 ...

  6. autohotkey-【GUI】Switch between Windows of the Same Application

    下面给出了ahk的脚本,但我需要GUI http://superuser.com/questions/435602/shortcut-in-windows-7-to-switch-between-sa ...

  7. overflow的劲爆知识点

    1.属性 visible(默认) hidden(此处是隐藏不是裁剪) scroll(滚动条) auto(智能路线 当超出范围时则出现滚动条) inherit 不常用  存在兼容性问题 2.进入CSS3 ...

  8. Linux下jdk的配置

    首先将*.tar.gz压缩包解压 命令:tar -xzvf *.tar.gz假设得到的文件夹为java 将其移动到/usr/中 命令为:sudo mv java /usr/ 然后设置环境变量: sud ...

  9. Linux 下多核CPU知识【转】

    转自:http://www.cnblogs.com/dongzhiquan/archive/2012/02/16/2354977.html 1. 在Linux下,如何确认是多核或多CPU: #cat ...

  10. 分析Linux内核创建一个新进程的过程【转】

    转自:http://www.cnblogs.com/MarkWoo/p/4420588.html 前言说明 本篇为网易云课堂Linux内核分析课程的第六周作业,本次作业我们将具体来分析fork系统调用 ...