405. Convert a Number to Hexadecimal

Easy

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.

Example 1:

Input:
26 Output:
"1a"

Example 2:

Input:
-1 Output:
"ffffffff"
package leetcode.easy;

public class ConvertANumberToHexadecimal {
char[] map = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; @org.junit.Test
public void test() {
System.out.println(toHex(26));
System.out.println(toHex(-1));
} public String toHex(int num) {
if (num == 0) {
return "0";
}
String result = "";
while (num != 0) {
result = map[(num & 15)] + result;
num = (num >>> 4);
}
return result;
}
}

LeetCode_405. Convert a Number to Hexadecimal的更多相关文章

  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] Convert a Number to Hexadecimal 数字转为十六进制

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

  3. Leetcode: Convert a Number to Hexadecimal

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

  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. [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal

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

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

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

  7. 【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', ...

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

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

  9. 405. Convert a Number to Hexadecimal

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

随机推荐

  1. Unicode原理和互转中文

    代码点Unicode标准的本意很简单:希望给世界上每一种文字系统的每一个字符,都分配一个唯一的整数,这些整数叫做代码点(Code Points). 代码空间所有的代码点构成一个代码空间(Code Sp ...

  2. Java输入流

    import java.util.*;  //java为小写public class TEST{ public static void main(String args[]){ Scanner inp ...

  3. [USACO15DEC]最大流Max Flow(树上差分)

    题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN st ...

  4. vs code 搭建java maven springboot环境

    Java Extension Pack,Maven for Java,Spring Boot Extension Pack https://blog.csdn.net/qq_26026975/arti ...

  5. 回调方式进行COM组件对外消息传递

    情景:被调用者--COM组件:调用者---外部程序作用:COM组件 到 外部程序 的消息传递方法: 1.外部程序通过接口类对象,访问接口类的方法.COM对象通过连接点方式,进行消息的反向传递. 2.外 ...

  6. luoguP2768: 珍珠项链(矩阵乘法优化DP)

    题意:有K种珍珠,每种N颗,求长度为1~N的项链,包含K种珍珠的项链种类数.N<=1e9, K<=30; 思路:矩阵快速幂,加个1累加前缀和即可. #include<bits/std ...

  7. C语言博客作业00--我的第一篇博客

    1.你对网络专业或者计算机专业了解是怎样? 起初 起初对于我来说,计算机专业毕业后就相当于程序员,或者去开发一些游戏,软件等等,而学得特别优秀的可能会成为黑客,就像电影电视剧里演得那样,这是我一开始的 ...

  8. ICEM-双管

    原视频下载地址:http://yunpan.cn/cLHCm7Uejw4eG  访问密码 b8a1

  9. 2015-2016-2《Java程序设计》团队博客2

     简易画图板介绍 一.功能结构图 二.主类设计 1.总体设计:在设计简易画图板时,根据程序功能的分类,包含了十二个文件,包括SimpleDraw.java,MenuContainer.java,Dra ...

  10. python爬虫 TapTap

    作业要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3075 对象 - TapTap TapTap 是一个高品质手游玩家社区, ...