File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 163163374|[ Guess Number Higher or Lower] ( ./0374-guess-number-higher-or-lower.js ) |Medium|
164164383|[ Ransom Note] ( ./0383-ransom-note.js ) |Easy|
165165387|[ 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|
166167414|[ Third Maximum Number] ( ./0414-third-maximum-number.js ) |Easy|
167168419|[ Battleships in a Board] ( ./0419-battleships-in-a-board.js ) |Medium|
168169442|[ Find All Duplicates in an Array] ( ./0442-find-all-duplicates-in-an-array.js ) |Medium|
You can’t perform that action at this time.
0 commit comments