Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. 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.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. 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进制)的更多相关文章

  1. 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 ...

  2. [leetcode] 405. Convert a Number to Hexadecimal

    https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...

  3. 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. [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 ...

  5. 405 Convert a Number to Hexadecimal 数字转换为十六进制数

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意:    十六进制中所有字母(a-f)都必须是小写.    十六进制字符串中不能包含多余的前导零.如果 ...

  6. 405. Convert a Number to Hexadecimal

    ..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...

  7. 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 ...

  8. LeetCode算法题-Convert a Number to Hexadecimal(Java实现)

    这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...

  9. [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

随机推荐

  1. 控制结构(2) 卫语句(guard clause)

    // 上一篇:分枝/叶子(branch/leaf) // 下一篇:状态机(state machine) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 典型代码: 同步版本 f ...

  2. Install Oracle 12c R2 on CentOS 7 silent

    准备工作 VMware 虚拟机 CentOS 7 17.08 系统安装包镜像 Oracle 12c R2 软件安装包 配置 yum 库并安装如下包 binutils-2.23.52.0.1-12.el ...

  3. Hibernate第十一篇【配置C3P0数据库连接池、线程Session】

    Hibernate连接池 Hibernate自带了连接池,但是呢,该连接池比较简单..而Hibernate又对C3P0这个连接池支持-因此我们来更换Hibernate连接池为C3P0 查看Hibern ...

  4. [03] Servlet继承关系和生命周期

    1.Servlet的继承关系 假如现有我们自定义的一个Servlet,继承HttpServlet,那么实际上它的继承链如下图:   可以看到,核心的部分在于: 两个顶级接口 Servlet Servl ...

  5. 将数组分割为几个等长度的子数组(使用slice)

    先了解一下slice方法: slice() 1.定义:slice()可从已有数组中截取返回指定的元素,形成一个新的数组: 语法:arrayObject.slice(start,end): 参数 描述 ...

  6. 记录兼容IE8中发现的一些问题

    1.new Date().getYear(); chrome下:获取的是1900年之后的年份,如2017年获取的是117 IE8下:获取的是公元年份,如2017获取的是2017 解决方案:使用new ...

  7. HDU-3032

    Problem Description Nim is a two-player mathematic game of strategy in which players take turns remo ...

  8. Day2 基本数据类型

    一.python数据类型 1.1数字 2 是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4 ...

  9. Spring 5:以函数式方式注册 Bean

    http://www.baeldung.com/spring-5-functional-beans 作者:Loredana Crusoveanu 译者:http://oopsguy.com 1.概述 ...

  10. Football 概率DP poj3071

                                                                                                 Footbal ...