-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathLeetcode405a.java
More file actions
47 lines (45 loc) · 1.62 KB
/
Copy pathLeetcode405a.java
File metadata and controls
47 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
/** 如果有多种方法, 可以将文件名和class 名字都改为 Leetcode1_method2或Leetcode1b 这种格式 */
class Leetcode405a {
public String toHex(int num) {
String res = "";
long N = num; // 预处理
if (N == 0)
return "0";
HashMap<Integer, Character> dict = new HashMap<Integer, Character>() {
{
put(0, '0');
put(1, '1');
put(2, '2');
put(3, '3');
put(4, '4');
put(5, '5');
put(6, '6');
put(7, '7');
put(8, '8');
put(9, '9');
put(10, 'a');
put(11, 'b');
put(12, 'c');
put(13, 'd');
put(14, 'e');
put(15, 'f');
}
};
if (N < 0) N = 4294967296L + N; /* 4294967296L就是 0x0000000100000000(16^8=2^32), Java中
* 不使用BigInteger无法存储该数, 只能hard code在这里了
*/
while (N > 0) {
long lastDigit = N % 16;
N /= 16;
res = dict.get((int) lastDigit) + res;
}
return res;
}
public static void main(String[] args) {
Leetcode405a sol = new Leetcode405a();
int num = 7;
String res = sol.toHex(num);
System.out.println(res);
}
}