-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path405.java
More file actions
31 lines (30 loc) · 1.12 KB
/
405.java
File metadata and controls
31 lines (30 loc) · 1.12 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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public String toHex(int num) {
if(num == 0) return "0";
StringBuilder sb = new StringBuilder();
while(num != 0) {
int digit = num & 0xf;
if(digit >= 0 && digit <= 9) sb.append(digit);
else sb.append((char)('a' + digit - 10));
num >>>= 4;
}
return sb.reverse().toString();
}
}
__________________________________________________________________________________________________
sample 35176 kb submission
class Solution {
public String toHex(int num) {
char[] map = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
if (num == 0) return "0";
StringBuilder ans = new StringBuilder();
while (num != 0) {
ans.append(map[num & 15]);
num >>>= 4;
}
return ans.reverse().toString();
}
}
__________________________________________________________________________________________________