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. Strong AI Versus Weak AI

    Computer Science An Overview _J. Glenn Brookshear _11th Edition The conjecture that machines can be ...

  2. ArcGIS Server 缓存服务增加新比例尺缓存

    win10 + Server 10.4 +  ArcMap 10.4  操作简单说明: ①窗口上方Customize栏→Toolbars→ Customize→ 搜索到 manege map serv ...

  3. php安装,mysql安装

    先安装mysql 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择“Source Code”,用已经注册好的oracle账 ...

  4. Git版本控制

    官方文档:http://git-scm.com/book/en/v2 github :https://guides.github.com/activities/hello-world/ How to ...

  5. NRF51822之发射功率

    设置蓝牙的TX Power 使用的函数sd_ble_gap_tx_power_set(int8_t tx_power);   参看例子为 S110/ble_app_proximity #define ...

  6. 算导Ch34. NP Complete

    1.图灵停机问题:无论在多长时间内都无法被任何一台计算机解决 问题描述:问题为H,H的输入数据为P(P是一段程序(程序也是一串字符串数据)),判定P在输入w下是否能够最终停止 H(P(w))=0 若P ...

  7. jfinal

    http://blog.csdn.net/zb0567/article/details/21083021

  8. Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the total sum of a

    class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class ...

  9. html5向左滑动删除特效

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. css3画图之大白(●—●)

    把大白送给你~ <!DOCTYPE html> <html> <head> <title>大白</title> <meta http- ...