Skip to content

Commit 79477e0

Browse files
Bit Manipulation: 405. Convert a Number to Hexadecimal
1 parent 39a385e commit 79477e0

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* [338](https://leetcode.com/problems/counting-bits/). Counting Bits - [code](src/bit_manipulation/easy/CountingBits.java)
4949
* [137](https://leetcode.com/problems/single-number-ii/). Single Number II - [code](src/bit_manipulation/easy/OneSingleNumberInGroupOfThreeNumbers.java)
5050
* [260](https://leetcode.com/problems/single-number-iii/). Single Number III - [code](src/bit_manipulation/easy/TwoSingleNumbers.java)
51+
* [405](https://leetcode.com/problems/convert-a-number-to-hexadecimal/). Convert a Number to Hexadecimal - [code](src/bit_manipulation/easy/ConvertANumberToHexadecimal.java)
5152
* [Gfg](https://practice.geeksforgeeks.org/problems/check-whether-k-th-bit-is-set-or-not-1587115620/1). Check whether K-th bit is set or not - [code](src/bit_manipulation/easy/CheckKthBitIsSet.java)
5253
* [Gfg](https://practice.geeksforgeeks.org/problems/odd-or-even3618/1). Odd or Even - [code](src/bit_manipulation/easy/OddOrEven.java)
5354
* [Gfg](https://practice.geeksforgeeks.org/problems/set-kth-bit3724/1). Set kth bit - [code](src/bit_manipulation/easy/SetKthBit.java)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package bit_manipulation.easy;
2+
3+
/***
4+
* Problem 405 in Leetcode: https://leetcode.com/problems/convert-a-number-to-hexadecimal/
5+
*
6+
* Given an integer num, return a string representing its hexadecimal representation.
7+
* For negative integers, two’s complement method is used.
8+
* All the letters in the answer string should be lowercase characters
9+
* and there should not be any leading zeros in the answer except for the zero itself.
10+
*
11+
* Note: You are not allowed to use any built-in library method to directly solve this problem.
12+
*
13+
* Example 1:
14+
* Input: num = 26
15+
* Output: "1a"
16+
*
17+
* Example 2:
18+
* Input: num = -1
19+
* Output: "ffffffff"
20+
*/
21+
22+
public class ConvertANumberToHexadecimal {
23+
private static final char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
24+
25+
public static void main(String[] args) {
26+
int num = -1;
27+
28+
System.out.println("Brute Force: " + convertIntoHexadecimalBruteForce(num));
29+
System.out.println("Bitwise: " + convertIntoHexadecimalBitwise(num));
30+
}
31+
32+
private static String convertIntoHexadecimalBruteForce(int num) {
33+
if (num == 0) {
34+
return "0";
35+
}
36+
37+
boolean isNegative = num < 0;
38+
39+
num = isNegative ? (num + 1) * -1 : num;
40+
41+
StringBuilder sb = new StringBuilder();
42+
43+
while (num > 0) {
44+
int r = num % 16;
45+
if (isNegative) {
46+
r = 15 - r;
47+
}
48+
char c = (char) (r < 10 ? r + '0' : r - 10 + 'a');
49+
sb.append(c);
50+
num /= 16;
51+
}
52+
53+
if (isNegative) {
54+
while (sb.length() < 8) {
55+
sb.append('f');
56+
}
57+
}
58+
59+
return sb.reverse().toString();
60+
}
61+
62+
private static String convertIntoHexadecimalBitwise(int num) {
63+
if (num == 0) {
64+
return "0";
65+
}
66+
67+
StringBuilder result = new StringBuilder();
68+
69+
while (num != 0) {
70+
int mask = 15;
71+
result.append(chars[(num & mask)]);
72+
num >>>= 4;
73+
}
74+
75+
return result.reverse().toString();
76+
}
77+
}

0 commit comments

Comments
 (0)