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. Hibernate的DetachedCriteria使用(含Criteria)

    1.背景了解:Hibernate的三种查询方式 Hibernate总的来说共有三种查询方式:HQL.QBC和SQL三种,这里做简单的概念介绍,不详细进行展开. 1.1 HQL(Hibernate Qu ...

  2. Linux硬链接软连接

    转载原文出处:http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html 1.Linux链接概念 Linux链接分两种,一种被称为硬链接( ...

  3. Apache POI

    Apache POI 用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"Po ...

  4. mybatis 自动生成代码(mybatis generator)

    pom.xml 文件配置 引入 mybatis generator <properties> <mysql.connector.version>5.1.44</mysql ...

  5. 常用Linux命令、包括vi 、svn

    /etc/init.d/network restart//===========================================更新脚本cd /www/scripts更新站点./sta ...

  6. SQLServer 自动循环归档分区数据脚本

    标签:SQL SERVER/MSSQL SERVER/数据库/DBA/表分区 概述 在很多业务场景下我们需要对一些记录量比较大的表进行分区,同时为了保证性能需要将一些旧的数据进行归档.在分区表很多的情 ...

  7. js两个叹号的使用

    1.浏览器判断空和未定义以及零时返回的值如下: alert(undefined)  //undefined alert(null)  //null alert(0)  //0 2.有时为了便于下一步判 ...

  8. struts2一个和多个文件上传及下载

    struts2的文件上传相比我们自己利用第三方jar包(commons-fileupload-1.2.1.jar   commons-io-1.3.2.jar )要简单的多,当然struts2里面也是 ...

  9. 新建maven项目遇到Select an Archetype时没有maven-archetype-webapp处理方法

    [已经有很多博客写过相关的了.详细请去看其他博主的.这里只是记录新建的时候发生的问题给新手提供帮助.因为我跟我的同事都遇到了.因为没记录下来,又花了时间找问题.而网上好像也不多.所以记录下来.希望帮到 ...

  10. Linux入门之常用命令(7)压缩

    compress filename 压缩   -d解压缩  *.Z bzip -d解压缩 -z压缩 *.bz2 bzcat filename .bz2 读取压缩文件内容 gzip -d解压缩  -#压 ...