Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e0145e5

Browse files
committed
Add solution #405
1 parent e4732ff commit e0145e5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 405. Convert a Number to Hexadecimal
3+
* https://leetcode.com/problems/convert-a-number-to-hexadecimal/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer num, return a string representing its hexadecimal
7+
* representation. For negative integers, two’s complement method is used.
8+
*
9+
* All the letters in the answer string should be lowercase characters,
10+
* and there should not be any leading zeros in the answer except for
11+
* the zero itself.
12+
*
13+
* Note: You are not allowed to use any built-in library method to directly
14+
* solve this problem.
15+
*/
16+
17+
/**
18+
* @param {number} num
19+
* @return {string}
20+
*/
21+
var toHex = function(num) {
22+
if (num === 0) {
23+
return num.toString();
24+
}
25+
const words = '0123456789abcdef';
26+
let result = '';
27+
while (num !== 0) {
28+
result = words[num & 0xf] + result;
29+
num >>>= 4;
30+
}
31+
return result;
32+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
164164
383|[Ransom Note](./0383-ransom-note.js)|Easy|
165165
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
166+
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
166167
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
167168
419|[Battleships in a Board](./0419-battleships-in-a-board.js)|Medium|
168169
442|[Find All Duplicates in an Array](./0442-find-all-duplicates-in-an-array.js)|Medium|

0 commit comments

Comments
 (0)