LeetCode_405. Convert a Number to Hexadecimal
405. 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"
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的更多相关文章
- 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] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- Leetcode: Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...
- 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 ...
- [Swift]LeetCode405. 数字转换为十六进制数 | Convert a Number to Hexadecimal
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❤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', ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- 405. Convert a Number to Hexadecimal
..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...
随机推荐
- 关于struct和typedef struct
以 struct TelPhone{ ]; ]; }; 为例 这里先定义了一个 TelPhone的结构体. 加入需要为TelPhone定义一个别名: 其语法为 typedef TelPhone TP: ...
- js插件---WebUploader 如何接收服务端返回的数据
js插件---WebUploader 如何接收服务端返回的数据 一.总结 一句话总结: uploadSuccess有两个参数,一个是file(上传的文件信息),一个是response(服务器返回的信息 ...
- 《逆袭团队》第八次团队作业:Alpha冲刺
项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业8:Alpha冲刺 团队名称 逆袭团队 具体目标 完成最后冲刺阶段的5次博客 一.团队项目github仓库地址:Github 二.Sc ...
- 格式化字符串——初级% 和format
print '{a},{b}'.format(a='hello',b='word') st='a %s %s x y z' st1=('b','c') print st%st1 print '%s % ...
- 爬虫 - 解析库之Beautiful Soup
了解Beautiful Soup 中文文档: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...
- iptables的使用
四表五链 四表(table):raw.mangle.nat.filter 五链(chain):PREROUTING.INPUT.FORWARD.OUTPUT.POSTROUTING 每个表存在几个或全 ...
- Net-NTLMv1的利用思路
Net-NTLMv1的加密方法: 客户端向服务器发送一个请求 服务器接收到请求后,生成一个16位的Challenge,发送回客户端 客户端接收到Challenge后,使用登录用户的密码hash对Cha ...
- 洛谷 P1717 钓鱼 题解
每日一题 day46 打卡 Analysis 首先通过题目我们不难发现,为了得到最优解,那么就不能把时间浪费在路上,也就是说不能走回头路.然后很容易可以发现,在每个时刻在不同的鱼塘钓到的鱼的数量是不同 ...
- 将 iTunes 降级到支持安装 .ipa 文件的版本
将 iTunes 降级到支持安装 .ipa 文件的版本 新版的 iTunes 再也不支持安装 .ipa 文件了,但是 Apple 官网依旧保留了旧版 iTunes 的下载渠道.(点击进入) 安装完上面 ...
- MuPAD使用总结
MuPAD使用总结 一.打开notebook界面的方法: 二.notebook界面的三种区域 (一).输入区域 输入区域在打开来的时候就有,就是,但是之后如果还想再加,可以点击上方红色框内的图标. 这 ...