您的位置:首页 > 其它

【LeetCode】Convert a Number to Hexadecimal 解题报告

2017-01-14 14:39 447 查看
[LeetCode]

https://leetcode.com/problems/convert-a-number-to-hexadecimal/

Difficulty: Easy

Question

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

Input:
26

Output:
"1a"

Input:
-1

Output:
"ffffffff"


Ways

只管想法就是把数字/16,然后从数组中取字符组成字符串。但这个只对正数有用,负数没法用。

对负数的处理不好办。刚开始想用Integer.MAX_VALUE减去负数得到整数,再转成16进制,但是,尝试了之后,正数的最大值得符号位是0,因此这个思路不同。

然后就是想到位运算。>>>的作用是无符号右移。每次右移4位就是相当于除以16,然后再把这个结果对16求余,即可。无论正负都可。因为这就是正确的每四位数划分求对应16进制数的方式。

这里有个技巧,就是hexs[(16 + num % 16) % 16],这样做的目的就是使正负数都能统一计算16进制,不会导致数组溢出。

public class Solution {
public String toHex(int num) {
char[] hexs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
StringBuilder answer = new StringBuilder();
if (num == 0) {
return "0";
}
while (num != 0) {
answer.insert(0, hexs[(16 + num % 16) % 16]);
num = num >>> 4;
}

return answer.toString();

}
}


AC: 8 ms

Date

2017 年 1 月 14 日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode