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

记住要delete begining 0

 public class Solution {
public String toHex(int num) {
StringBuffer res = new StringBuffer();
String[] charMap = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
for (int i=0; i<8; i++) {
int number = num & (0b1111);
num = num >>> 4;
res.insert(0, charMap[number]);
}
while (res.charAt(0)=='0' && res.length()>1) res.deleteCharAt(0);
return res.toString();
}
}

要记住单独处理 num == 0的情况

 public class Solution {
public String toHex(int num) {
if (num == 0) return "0";
StringBuffer res = new StringBuffer();
String[] charMap = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
while (num != 0) {
int number = num & (0b1111);
num = num >>> 4;
res.insert(0, charMap[number]);
}
return res.toString();
}
}

Leetcode: Convert a Number to Hexadecimal的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  8. 【leetcode❤python】Convert a Number to Hexadecimal

    #-*- coding: UTF-8 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6', ...

  9. [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal

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

随机推荐

  1. regardless of how many processors are devoted to a parallelized execution of this program

    https://en.wikipedia.org/wiki/Amdah's_law Amdahl's law is often used in parallel computing to predic ...

  2. 【php学习】时间函数

    手工画了一张图,来大体概括php中对于时间的处理函数 首先时间戳是这样“1441202665”的一串数字,虽然人看起来麻烦,但是计算机却很容易识别这样的时间表示形式. 所以给计算机看的时间是时间戳,给 ...

  3. EBS 资源路径

    /data03/DEV4/comn/java/cmcc/upload cd  $OA_HTML /data03/DEV4/comn/html 日志 $IAS_ORACLE_HOME/Apache/Js ...

  4. Squid 操作实践

    Squid简介 Squid可以做什么 性能要素 Squid安装 Squid快速体验 Squid配置 Squid简介 Squid is a caching proxy for the Web suppo ...

  5. 为mutable类型的容器(array,set等)添加kvo,有点麻烦,供参考和了解下吧

    http://blog.csdn.net/caryaliu/article/details/49284185 需要在被观察的属性所在的类里面实现一些方法,对开发者不友好,一般不建议使用,这里mark一 ...

  6. cdecl和stdcall调用约定-汇编演示

    . .model flat, stdcall .stack ExitProcess PROTO, dwExitCode:DWORD .data val2 sdword result dword ? . ...

  7. UML学习

    学习链接:http://blog.csdn.net/wangyongxia921/article/category/1293975 感谢原文作者.

  8. Maven实战(七)settings.xml相关配置

    一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们 ...

  9. detailsview 样式小问题

    detailsview不显示表头,设置HeaderText=""就可以,不知道为什么,如果有值,其他方式都没有调好! 内网格显示为0,Gridlines="None&qu ...

  10. The Four Stages of Recovering a Project

    If a project is in trouble, the project manager needs to work to recover it and get the schedule bac ...