Skip to content

Commit 0b8cd34

Browse files
committed
leetcode
1 parent 852cd1b commit 0b8cd34

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
-* 405. Convert a Number to Hexadecimal *-
4+
5+
Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
6+
7+
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
8+
9+
Note: You are not allowed to use any built-in library method to directly solve this problem.
10+
11+
12+
13+
Example 1:
14+
15+
Input: num = 26
16+
Output: "1a"
17+
Example 2:
18+
19+
Input: num = -1
20+
Output: "ffffffff"
21+
22+
23+
Constraints:
24+
25+
-231 <= num <= 231 - 1
26+
27+
28+
*/
29+
String toHexString(int num) {
30+
if (num == 0) {
31+
return "0";
32+
}
33+
34+
int n = num.toUnsigned(32);
35+
final String ref = "0123456789abcdef";
36+
37+
String result = "";
38+
39+
while (n != 0) {
40+
result = ref[n & 0xF] + result;
41+
n >>= 4;
42+
}
43+
44+
return result;
45+
}
46+
47+
String toHex(int num) {
48+
return (num < 0)
49+
? (num.toUnsigned(32)).toRadixString(16)
50+
: num.toRadixString(16);
51+
}
52+
53+
extension ToHex on int {
54+
String toHexString(int num) {
55+
if (num == 0) {
56+
return "0";
57+
}
58+
59+
int n = num.toUnsigned(32);
60+
final String ref = "0123456789abcdef";
61+
62+
String result = "";
63+
64+
while (n != 0) {
65+
result = ref[n & 0xF] + result;
66+
n >>= 4;
67+
}
68+
69+
return result;
70+
}
71+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
func toHex(num int) string {
4+
if num == 0 {
5+
return "0"
6+
}
7+
8+
const bitsPerDigit = 4
9+
const mask = (1 << bitsPerDigit) - 1
10+
11+
hex := make([]byte, 8)
12+
i := 7
13+
14+
// Convert negative numbers to their two's complement representation
15+
if num < 0 {
16+
num = (1 << 32) + num
17+
}
18+
19+
for num != 0 {
20+
nib := num & mask
21+
hex[i] = getHexDigit(nib)
22+
num >>= bitsPerDigit
23+
i--
24+
}
25+
26+
return string(hex[i+1:])
27+
}
28+
29+
func getHexDigit(n int) byte {
30+
if n < 10 {
31+
return byte('0' + n)
32+
}
33+
return byte('a' + n - 10)
34+
}
35+
36+
//--------------------------
37+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 🔥 Number to Hex 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
String toHex(int num) {
7+
return (num < 0)
8+
? (num.toUnsigned(32)).toRadixString(16)
9+
: num.toRadixString(16);
10+
}
11+
```
12+
13+
### Extra - Avoid It (Not-Optimized)
14+
15+
```dart
16+
extension ToHex on int {
17+
String toHex() {
18+
return (this < 0)
19+
? (this.toUnsigned(32)).toRadixString(16)
20+
: this.toRadixString(16);
21+
}
22+
}
23+
```
24+
25+
## Solution - 2
26+
27+
```dart
28+
String toHexString(int num) {
29+
if (num == 0) {
30+
return "0";
31+
}
32+
33+
int n = num.toUnsigned(32);
34+
final String ref = "0123456789abcdef";
35+
36+
String result = "";
37+
38+
while (n != 0) {
39+
result = ref[n & 0xF] + result;
40+
n >>= 4;
41+
}
42+
43+
return result;
44+
}
45+
```
46+
47+
### Extra (Optimized Version - BitManipulation)
48+
49+
```dart
50+
extension ToHex on int {
51+
String toHexString(int num) {
52+
if (num == 0) {
53+
return "0";
54+
}
55+
56+
int n = num.toUnsigned(32);
57+
final String ref = "0123456789abcdef";
58+
59+
String result = "";
60+
61+
while (n != 0) {
62+
result = ref[n & 0xF] + result;
63+
n >>= 4;
64+
}
65+
66+
return result;
67+
}
68+
}
69+
```
70+
71+
### Bonus
72+
73+
To Use on Any Number In Our case we can use only on int.
74+
But for bonus I have implemented for both double as well as int.
75+
InShort For NUM use can use DART Generics if you want but not recommended
76+
77+
### [Source](https://github.com/ayoubzulfiqar/dart-utils/blob/main/extensions/to_hex.dart)
78+
79+
### [GitHub](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)