【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)
【leetcode❤python】Convert a Number to Hexadecimal的更多相关文章
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- 【leetcode❤python】 9. Palindrome Number
#回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object): ...
- 【leetcode❤python】263. Ugly Number
class Solution(object): def isUgly(self, num): if num<=0:return False comlist=[2 ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【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 ...
- 【leetcode❤python】191. Number of 1 Bits
#-*- coding: UTF-8 -*- class Solution(object): def hammingWeight(self, n): if n<=0:retu ...
- 【leetcode❤python】171. Excel Sheet Column Number
#-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string. ...
- 【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 ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
随机推荐
- android怎么换背景图片
我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类 ...
- 2. 星际争霸之php面向对象(二)
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- MyEclipse启动失败
日志的一部分: !SESSION 2014-09-24 11:47:03.156 -----------------------------------------------eclipse.buil ...
- 【sublime】解决汉字输入的办法——InputHelper;在sublime中输入汉字==》InputHelper方法
手工安装办法: cd ~/.config/sublime-text-2/Packages #如果是sublime3就改一下 git clone https://github.com/xgenvn/I ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- 关于 ActiveMQ 的消息模式
1.JMS Queue 执行 load balancer语义:一条消息仅能被一个 consumer(消费者) 收到.如果在 message 发送的时候没有可用的consumer,那么它将被保存一直到能 ...
- TI CC254x BLE教程 3
通用属性配置(Generic Attribute Profiles) 1. 数据在services里面以特征字(characteristics)的形式展示出来 如你所见, 特征字包含属性(Proper ...
- linux的mtd架构分析【转】
转自:http://blog.csdn.net/column/details/xgbing-linux-mtd.html linux mtd 嵌入式系统的存储有很多不可靠之处.随着使用容量的增大,现在 ...
- UserSelector兼容
1.更新到asp.net2.0或以上,将Microsoft.Web.UI.TreeView更换为新的System.Web.UI.WebControls.TreeView 2.将UserId,UserT ...
- 面向生产环境的大集群模式安装Hadoop
一.实验说明 1.本实验将使用DNS而不是hosts文件解析主机名: 2.使用NFS共享密钥文件,而不是逐个手工拷贝添加密钥: 3.复制Hadoop时使用批量拷贝脚本而不是逐台复制. 测试环境: Ho ...