[LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
- All letters in hexadecimal (
a-f
) must be in lowercase. - The hexadecimal string must not contain extra leading
0
s. If the number is zero, it is represented by a single zero character'0'
; otherwise, the first character in the hexadecimal string will not be the zero character. - The given number is guaranteed to fit within the range of a 32-bit signed integer.
- You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:
Input:
26 Output:
"1a"
Example 2:
Input:
-1 Output:
"ffffffff"
Code T: O(1)
class Solution:
def toHex(self, num):
ans, d = "", {10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f'}
if num == 0:
return ""
if num < 0:
num = 2**32 + num
while num > 0:
tem, rem = divmod(num, 16)
if rem > 9:
ans += d[rem]
else:
ans += str(rem)
num = tem
return ans[::-1]
[LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation的更多相关文章
- 38. leetcode 405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecim ...
- LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果 ...
- 405. Convert a Number to Hexadecimal
..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...
- LeetCode 1290. Convert Binary Number in a Linked List to Integer
题目 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN ...
- LeetCode_405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Easy Given an integer, write an algorithm to convert it to hexa ...
- how to convert a number to a number array in javascript without convert number to a string
how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...
随机推荐
- python 中的 print 函数与 list函数
print() 函数: 传入单个参数时默认回车换行,关键词 end 可以用来避免输出后的回车(换行), 或者以一个不同的字符串结束输出. >>> a, b = 0, 1 >& ...
- JQuery登录代码
$(function () { $("#login").submit(function(event) { event.preventDefault(); if ($("# ...
- MySql 远程连接的条件
1.首先看服务器防火墙 引用:http://www.cnblogs.com/silent2012/archive/2015/07/28/4682770.html CentOS 7.0默认使用的是fir ...
- PHP利用反射根据类名反向寻找类所在文件
有时候分析源码时,会被博大精深的层层代码搞得晕头转向,不知道类是定义在哪个文件里的,有时候IDE所提供的方法声明未必准确.在这种情况下,我们可以利用反射找到类所在的文件. 在你发现实例化类的地方(例如 ...
- virtuanenv+flask
1.virtualenv&flask 专门为特定项目创建一个目录和一个虚拟的Python 运行环境 # 1.安装 virtualenv$ pip3 install virtualenv #.创 ...
- 拳打Adam,脚踢SGD:北大提出全新优化算法AdaBound
https://mp.weixin.qq.com/s/el1E-61YjLkhFd6AgFUc7w
- opencv -python
https://www.python----------tab.com/html/2017/pythonhexinbiancheng_1120/1184.html http://www.cnblogs ...
- Chap5:数字货币交易[《区块链中文词典》维京&甲子]
- CABasicAnimation 划线动画
CGFloat animateDuration = ; UIBezierPath *bezierPath = [[UIBezierPath alloc] init]; CGPoint centerFr ...
- 深度剖析fork()的原理及用法
我们都知道通过fork()系统调用我们可以创建一个和当前进程印象一样的新进程.我们通常将新进程称为子进程,而当前进程称为父进程.而子进程继承了父进程的整个地址空间,其中包括了进程上下文,堆栈地址,内存 ...