Leetcode: Convert a Number to Hexadecimal
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的更多相关文章
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- LeetCode算法题-Convert a Number to Hexadecimal(Java实现)
这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...
- 【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', ...
- [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
随机推荐
- [转]为何TCP/IP协议栈设计成沙漏型的
http://m.blog.csdn.net/blog/dog250/18959371 前几天有人回复我的一篇文章问,为何TCP/IP协议栈设计成沙漏型的.这个问题问得好!我先不谈为何它如此设计,我一 ...
- Translation Lookaside Buffer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In principle, then, e ...
- [收藏]NET技术+25台服务器怎样支撑世界第54大网站
.NET技术+25台服务器怎样支撑世界第54大网站 英文原文:StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's ...
- Solr4.3之检索建议suggest
原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...
- Redis学习笔记(1)-Key
package cn.com; import java.text.ParseException; import java.util.List; import java.util.Set; import ...
- ThreadPoolExecutor 分析
一.从用法入手 Creates a thread pool that creates new threads as needed, but will reuse previously construc ...
- 小米1plus MIUI RadioButton的问题
小米1plus MIUI RadioButton不能设置setBackground(Drawable drawable);会变成黑色背景,需要单独处理
- Java冒泡排序,Java对象冒泡排序
今天呆公司特别无聊,百度了一下Java机试题,看到一个冒泡排序. 粘上我全部的代码: 实体类: package accp.com.internet;/** * 人物类 * @author xuxiao ...
- JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...
- 【转】ViewPager实现一个页面多个Item的显示
转自:http://billyyuan.iteye.com/blog/1941538 ViewPager实现一个页面多个Item的显示 博客分类: android 代码在: https://cod ...