Skip to content

Commit 9dc83f1

Browse files
committed
solve problem Convert A Number To Hexadecimal
1 parent 2e8bf94 commit 9dc83f1

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ All solutions will be accepted!
118118
|744|[Find Smallest Letter Greater Than Target](https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target/description/)|[java/py/js](./algorithms/FindSmallestLetterGreaterThanTarget)|Easy|
119119
|203|[Remove Linked List Elements](https://leetcode-cn.com/problems/remove-linked-list-elements/description/)|[java/py/js](./algorithms/RemoveLinkedListElements)|Easy|
120120
|237|[Delete Node In A Linked List](https://leetcode-cn.com/problems/delete-node-in-a-linked-list/description/)|[java/py/js](./algorithms/DeleteNodeInALinkedList)|Easy|
121+
|405|[Convert A Number To Hexadecimal](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/)|[java/py/js](./algorithms/ConvertANumberToHexadecimal)|Easy|
121122

122123
# Database
123124
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Convert A Number To Hexadecimal
2+
This problem is easy to solve by bitwise, pay attention to the bit spillover
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String toHex(int num) {
3+
long longNum = num;
4+
if (longNum == 0) {
5+
return "0";
6+
}
7+
8+
if (longNum < 0) {
9+
// use 4294967295L to replace 0xffffffff
10+
// because 0xffffffff in java is -1
11+
longNum = 4294967295L + num + 1;
12+
}
13+
14+
String res = "";
15+
16+
while (longNum > 0) {
17+
int mod = (int) (longNum % 16);
18+
if (mod >= 10) {
19+
res = String.valueOf(((char)(mod + 87))) + res;
20+
} else {
21+
res = String.valueOf(mod) + res;
22+
}
23+
longNum = longNum / 16L;
24+
}
25+
26+
return res;
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
var toHex = function(num) {
6+
if (num === 0) {
7+
return '0'
8+
}
9+
10+
if (num < 0) {
11+
// use + replace ^
12+
// + means - abs of num
13+
num = 0xffffffff + num + 1
14+
}
15+
16+
let res = ''
17+
while (num > 0) {
18+
let mod = num % 16
19+
if (mod >= 10) {
20+
res = String.fromCharCode(mod + 87) + res
21+
} else {
22+
res = String(mod) + res
23+
}
24+
25+
num = parseInt(num / 16)
26+
}
27+
28+
return res
29+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def toHex(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
if num == 0:
8+
return '0'
9+
10+
if num < 0:
11+
num = (-num ^ 0xffffffff) + 1
12+
res = ''
13+
while num > 0:
14+
mod = num % 16
15+
if mod >= 10:
16+
res = chr(mod + 87) + res
17+
else:
18+
res = str(mod) + res
19+
num = num / 16
20+
return res

0 commit comments

Comments
 (0)