Skip to content

Commit 915e2e1

Browse files
refactor 405
1 parent 548de0b commit 915e2e1

File tree

1 file changed

+2
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-31
lines changed

src/main/java/com/fishercoder/solutions/_405.java

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 405. Convert a Number to Hexadecimal
5-
6-
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
7-
8-
Note:
9-
10-
All letters in hexadecimal (a-f) must be in lowercase.
11-
The hexadecimal string must not contain extra leading 0s.
12-
If the number is zero, it is represented by a single zero character '0';
13-
otherwise, the first character in the hexadecimal string will not be the zero character.
14-
The given number is guaranteed to fit within the range of a 32-bit signed integer.
15-
You must not use any method provided by the library which converts/formats the number to hex directly.
16-
17-
Example 1:
18-
Input:
19-
26
20-
21-
Output:
22-
"1a"
23-
24-
Example 2:
25-
Input:
26-
-1
27-
28-
Output:
29-
"ffffffff"
30-
31-
*/
323
public class _405 {
334

345
public static class Solution1 {
356
public String toHex(int num) {
367
char[] hexChars =
37-
new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
38-
'e', 'f'};
8+
new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
9+
'e', 'f'};
3910
String result = "";
4011
while (num != 0) {
4112
result = hexChars[(num & 15)] + result;

0 commit comments

Comments
 (0)