Skip to content

Commit 322688c

Browse files
solves convert a number to hexadecimal in python
1 parent 7226d68 commit 322688c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
| 392 | [Is Subsequence](https://leetcode.com/problems/is-subsequence) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsSubsequence.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_subsequence.py) |
109109
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BinaryWatch.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/binary_watch.py) |
110110
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SumOfLeftLeaves.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/sum_of_left_leaves.py) |
111-
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ConvertNumberToHexadecimal.java) |
111+
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ConvertNumberToHexadecimal.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/convert_a_number_to_hexadecimal.py) |
112112
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation) | Easy | |
113113
| 409 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LongestPalindrome.java) |
114114
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/FizzBuzz.java) |
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def __init__(self):
3+
self.hex_chars = [str(i) for i in range(10)] + [chr(97 + i) for i in range(7)]
4+
5+
def toHex(self, num: int) -> str:
6+
if num == 0:
7+
return '0'
8+
if num < 0:
9+
num = 2 ** 32 + num
10+
result = ''
11+
while num != 0:
12+
result = self.hex_chars[num & 15] + result
13+
num = (num >> 4)
14+
return result

0 commit comments

Comments
 (0)