LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
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.
题目标签:Bit Manipulation
这道题目给了我们一个int 数字,我们需要把它转化成16进制,并且要把leading zeros都去掉。首先设立一个map把10-a, 11-b, 12-c,13-d,14-e,15-f 存入map。设一个for loop走32次, 因为16进制是4个bits为一组,所以这个loop可以设为i=i+4;然后每一次loop,需要一个进制位数,1,2,4,8, 利用num & 1把最右边的bit 拿出来 * 进制位数(1,2,4,8),再利用 >> 1 把bits往右移一位。当4格bits的总和知道以后,如果比10小,直接保存,如果大于等于10,就去map里找到对应的值存入。最后一步就是去掉leading zeros。
Java Solution:
Runtime beats 27.25%
完成日期:06/28/2017
关键词:Bit Manipulation
关键点:利用 & 1拿到bit, 利用 >> 来移动bits
public class Solution
{
public String toHex(int num)
{
if(num == 0)
return "0"; HashMap<Integer, String> map = new HashMap<>();
StringBuilder str = new StringBuilder();
String res = ""; map.put(10, "a");
map.put(11, "b");
map.put(12, "c");
map.put(13, "d");
map.put(14, "e");
map.put(15, "f"); for(int i=0; i<31; i=i+4) // iterate 32 bits
{
int sum = 0;
for(int j=1; j<=8; j=j*2) // get 4 bits sum
{
sum += (num & 1) * j;
num = num >> 1;
} if(sum < 10)
str.insert(0, sum);
else
{
str.insert(0, map.get(sum));
}
} res = str.toString();
// get rid of leading zeros
for(int i=0; i<res.length(); i++)
{
if(res.charAt(i) != '0')
{
res = res.substring(i);
break;
}
} return res;
}
}
参考资料:
https://stackoverflow.com/questions/2800739/how-to-remove-leading-zeros-from-alphanumeric-text
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)的更多相关文章
- 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
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 ...
- [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 compl ...
- 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_405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Easy Given an integer, write an algorithm to convert it to hexa ...
- LeetCode算法题-Convert a Number to Hexadecimal(Java实现)
这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
随机推荐
- 参考:Python 调试方法
地址:http://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ 这是Python代码调试技巧,也是我今天从别的地方看到的,然后转载 ...
- jstl-初步认知
JSTL是java提供的JSP标签库 1,在项目中加入 jsf-api.jar jsf-impl.jar jstl-1.2.jar 三个包 2, 如何在jsp页面引入标签库 使用 <@tagli ...
- JDBC操作数据库之连接数据库
通过JDBC向数据库中添加数据的时候,使用insert语句实现数据的插入,再SQL语句中的参数可以用占位符"?"来替代,然后通过PreparedStatement对象或者State ...
- [android游戏开发初学]SurfaceView初探之缓冲区测试
先上测试代码 private static class PathView extends SurfaceView implements SurfaceHolder.Callback{ private ...
- 关于Tomcat一些启动错误的解决方法
一.Eclipse tomcat 启动超时: 错误内容: Server JBoss v4.0 at localhost was unable to start within 50 seconds. I ...
- Https系列之一:https的简单介绍及SSL证书的生成
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- PHP 生成毫秒时间戳
PHP的time()函数生成当前时间的秒数,但是在一些情况下我们需要获取当前服务器时间和GMT(格林威治时间)1970年1月0时0分0秒的毫秒数,与Java中的currentTimeMilis()函数 ...
- 使用Xshell+Xmanager远程监控jvisualvm
使用jvisualvm的remote方式监控服务器端jvisualvm时,不是很方便,因此通过local方式,应该是正路. 一.服务器端(Linux,最小安装模式,没有图形界面) 1.安装xauth ...
- poj3070矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7752 Accepted: 5501 Descrip ...
- python---random模块使用详解
random与随机操作有关的模块 常用方法: random() --- 返回0-1之见得一个随机浮点数. 调用:random.random() 例如: >>> random.rand ...