Skip to content

Commit ca870d5

Browse files
committed
401_Binary_Watch and 405_Convert_a_Number_to_Hexadecimal
1 parent 2d3422f commit ca870d5

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Python & JAVA Solutions for Leetcode (inspired by [haoel's leetcode](https://github.com/haoel/leetcode))
22

3-
[Python](https://github.com/qiyuangong/leetcode/tree/master/python) and [Java](https://github.com/qiyuangong/leetcode/tree/master/java) full list
3+
Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to [jwasham's coding-interview-university](https://github.com/jwasham/coding-interview-university).
44

5-
♥ means you need a subscription.
5+
[Python](https://github.com/qiyuangong/leetcode/tree/master/python) and [Java](https://github.com/qiyuangong/leetcode/tree/master/java) full list. ♥ means you need a subscription.
66

77
| # | Title | Solution | Basic idea (One line) |
88
|---| ----- | -------- | --------------------- |
@@ -118,7 +118,9 @@
118118
| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/387_First_Unique_Character_in_a_String.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/387_First_Unique_Character_in_a_String.java) | Get frequency of each letter, return first letter with frequency 1, O(n) and O(1) |
119119
| 388 | [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/388_Longest_Absolute_File_Path.py) | Store last length and rindex, O(n) and O(n) |
120120
| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/389_Find_the_Difference.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/389_Find_the_Difference.java) | 1. Imaging letter a as 0, then the sum(t)-sum(s) is the result<br> 2. Based on solution 1, bit manipulate |
121+
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/401_Binary_Watch.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/401_Binary_Watch.java) | Note that 12 * 60 is much less than 2^n or n^2. |
121122
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/404_Sum_of_Left_Leaves.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/404_Sum_of_Left_Leaves.java) | 1. Recursively DFS with root.left.left and root.left.right check<br>2. The same DFS based on stack |
123+
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/405_Convert_a_Number_to_Hexadecimal.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/405_Convert_a_Number_to_Hexadecimal.java) | [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) 1. Bit manipulate, each time handle 4 digits<br>2. Python (hex) and Java API (toHexString & Integer.toHexString) |
122124
| 408 | [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/408_Valid_Word_Abbreviation.py) | Go over abbr and word, O(n) and O(1) |
123125
| 416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/416_Partition_Equal_Subset_Sum.py) | DP, Check if sum of some elements can be half of total sum, O(total_sum / 2 * n) and O(total_sum / 2) |
124126
| 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/421_Maximum_XOR_of_Two_Numbers_in_an_Array.py) | Check 0~32 prefix, check if there is x y in prefixes, where x ^ y = answer ^ 1, O(32n) and O(n) |

java/401_Binary_Watch.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public List<String> readBinaryWatch(int num) {
3+
List<String> times = new ArrayList<>();
4+
for (int h = 0; h < 12; h++)
5+
for (int m = 0; m < 60; m++)
6+
if (Integer.bitCount(h * 64 + m) == num)
7+
times.add(String.format("%d:%02d", h, m));
8+
return times;
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
2+
3+
class Solution {
4+
public String toHex(int num) {
5+
String hex_map = "0123456789abcdef";
6+
if (num == 0) return "0";
7+
String res = "";
8+
// To avoid infinite loop caused by negative num
9+
while (num != 0 && res.length() < 8) {
10+
res = hex_map.charAt(num & 15) + res;
11+
num = num >> 4;
12+
}
13+
return res;
14+
}
15+
16+
/* public String toHex(int num) {
17+
String hex = Integer.toHexString(num);
18+
return hex;
19+
} */
20+
}

python/401_Binary_Watch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def readBinaryWatch(self, num):
3+
"""
4+
:type num: int
5+
:rtype: List[str]
6+
"""
7+
return ['%d:%02d' % (h, m)
8+
for h in range(12) for m in range(60)
9+
if (bin(h) + bin(m)).count('1') == num]
10+
11+
12+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
class Solution(object):
3+
def toHex(self, num):
4+
"""
5+
:type num: int
6+
:rtype: str
7+
"""
8+
if num == 0:
9+
return '0'
10+
# letter map
11+
mp = '0123456789abcdef'
12+
ans = ''
13+
for _ in range(8):
14+
# get last 4 digits
15+
# num & 1111b
16+
n = num & 15
17+
# hex letter for current 1111
18+
c = mp[n]
19+
ans = c + ans
20+
# num = num / 16
21+
num = num >> 4
22+
#strip leading zeroes
23+
return ans.lstrip('0')
24+
25+
# def toHex(self, num):
26+
# def tohex(val, nbits):
27+
# return hex((val + (1 << nbits)) % (1 << nbits))
28+
# return tohex(num, 32)[2:]
29+
30+
# def toHex(self, num, h=''):
31+
# return (not num or h[7:]) and h or self.toHex(num / 16, '0123456789abcdef'[num % 16] + h)

0 commit comments

Comments
 (0)