[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 如何在不将数字转换 ...
随机推荐
- iOS-Core Animation: 变换
仿射变换 用 CGPoint 的每一列和 CGAffineTransform 矩阵的每一行对应元素相乘再求 和,就形成了一个新的 CGPoint 类型的结果.要解释一下图中显示的灰色元素, 为了能让矩 ...
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- jdbc ---- DBUTilDao 类
1, 列用工具包 阿里的 DbUtils: JDBC Utility Component Examples 再次封装成通用的 update, query package com.ljs.dao; i ...
- [No000017C]改善C#程序的建议5:引用类型赋值为null与加速垃圾回收
在标准的Dispose模式中(见前一篇博客“C#中标准Dispose模式的实现”),提到了需要及时释放资源,却并没有进一步细说让引用等于null是否有必要. 有一些人认为等于null可以帮助垃圾回收机 ...
- tensorRT使用python进行网络定义
- vue 错误分析
1 点击事件发生的错误 原因是,重复触发事件函数导致 改为不一样的名字即可 2 提示 : “ vuex] Expects string as the type, but found undef ...
- 直和 direct sum
小结: 1.xy平面 与 直和 https://en.wikipedia.org/wiki/Direct_sum For example, the xy-plane, a two-dimension ...
- 20个有趣的Linux命令
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- 最大似然估计(Maximum likelihood estimation)(通过例子理解)
似然与概率 https://blog.csdn.net/u014182497/article/details/82252456 在统计学中,似然函数(likelihood function,通常简写为 ...
- python之if __name__ == '__main__'
if __name__ == '__main__' 我们简单的理解就是: 如果模块是被直接运行的,则代码块被运行,如果模块是被导入的,则代码块不被运行.