Skip to content

Commit ed84039

Browse files
committed
solve problem Binary Watch
1 parent 51cd400 commit ed84039

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ All solutions will be accepted!
171171
|400|[Nth Digits](https://leetcode-cn.com/problems/nth-digit/description/)|[java/py/js](./algorithms/NthDigits)|Easy|
172172
|160|[Intersection Of Two Linked Lists](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/)|[java/py/js](./algorithms/IntersectionOfLinkedLists)|Easy|
173173
|860|[LemonadeChange](https://leetcode-cn.com/problems/lemonade-change/description/)|[java/py/js](./algorithms/LemonadeChange)|Easy|
174+
|401|[Binary Watch](https://leetcode-cn.com/problems/binary-watch/description/)|[java/py/js](./algorithms/BinaryWatch)|Easy|
174175

175176
# Database
176177
|#|Title|Solution|Difficulty|

algorithms/BinaryWatch/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Binary Watch
2+
This problem is easy to solve

algorithms/BinaryWatch/Solution.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public List<String> readBinaryWatch(int num) {
3+
Map<Integer, String[]> hoursMap = new HashMap<Integer, String[]>(),
4+
minsMap = new HashMap<Integer, String[]>();
5+
6+
hoursMap.put(0, new String[]{"0"});
7+
hoursMap.put(1, new String[]{"1", "2", "4", "8"});
8+
hoursMap.put(2, new String[]{"3", "5", "9", "6", "10"});
9+
hoursMap.put(3, new String[]{"7", "11"});
10+
11+
minsMap.put(0, new String[]{"00"});
12+
minsMap.put(1, new String[]{"01", "02", "04", "08", "16", "32"});
13+
minsMap.put(2, new String[]{"03", "05", "09", "17", "33", "06", "10", "18", "34", "12", "20", "36", "24", "40", "48"});
14+
minsMap.put(3, new String[]{"07", "11", "19", "35", "13", "21", "37", "25", "41", "49", "14", "22", "38", "26", "42", "50", "28", "44", "52", "56"});
15+
minsMap.put(4, new String[]{"15", "23", "39", "27", "43", "51", "29", "45", "53", "57", "30", "46", "54", "58"});
16+
minsMap.put(5, new String[]{"31", "47", "55", "59"});
17+
18+
List<String> res = new ArrayList<String>();
19+
20+
for (int i = 0; i < 4; i++) {
21+
if (minsMap.get(num - i) != null) {
22+
for (String hour : hoursMap.get(i)) {
23+
for (String min : minsMap.get(num - i)) {
24+
res.add(hour + ":" + min);
25+
}
26+
}
27+
}
28+
}
29+
30+
return res;
31+
}
32+
}

algorithms/BinaryWatch/solution.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} num
3+
* @return {string[]}
4+
*/
5+
var readBinaryWatch = function(num) {
6+
let preHoursMap = {
7+
0: ['0'],
8+
1: ['1', '2', '4', '8'],
9+
2: ['3', '5', '9', '6', '10'],
10+
3: ['7', '11']
11+
}
12+
let preMinsMap = {
13+
0: ['00'],
14+
1: ['01', '02', '04', '08', '16', '32'],
15+
2: ['03', '05', '09', '17', '33', '06', '10', '18', '34', '12', '20', '36', '24', '40', '48'],
16+
3: ['07', '11', '19', '35', '13', '21', '37', '25', '41', '49', '14', '22', '38', '26', '42', '50', '28', '44', '52', '56'],
17+
4: ['15', '23', '39', '27', '43', '51', '29', '45', '53', '57', '30', '46', '54', '58'],
18+
5: ['31', '47', '55', '59']
19+
}
20+
21+
let res = []
22+
23+
for (let i = 0; i < 4; i++) {
24+
if (preMinsMap[num - i]) {
25+
preHoursMap[i].forEach(hour => {
26+
preMinsMap[num - i].forEach(min => res.push(hour + ':' + min))
27+
})
28+
}
29+
}
30+
31+
return res
32+
};

algorithms/BinaryWatch/solution.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def readBinaryWatch(self, num):
3+
"""
4+
:type num: int
5+
:rtype: List[str]
6+
"""
7+
pre_hours_map = {
8+
0: ['0'],
9+
1: ['1', '2', '4', '8'],
10+
2: ['3', '5', '9', '6', '10'],
11+
3: ['7', '11']
12+
}
13+
pre_mins_map = {
14+
0: ['00'],
15+
1: ['01', '02', '04', '08', '16', '32'],
16+
2: ['03', '05', '09', '17', '33', '06', '10', '18', '34', '12', '20', '36', '24', '40', '48'],
17+
3: ['07', '11', '19', '35', '13', '21', '37', '25', '41', '49', '14', '22', '38', '26', '42', '50', '28', '44', '52', '56'],
18+
4: ['15', '23', '39', '27', '43', '51', '29', '45', '53', '57', '30', '46', '54', '58'],
19+
5: ['31', '47', '55', '59']
20+
}
21+
22+
res = []
23+
24+
for i in range(4):
25+
if pre_mins_map.get(num - i):
26+
for x in pre_hours_map.get(i):
27+
for y in pre_mins_map.get(num - i):
28+
res.append(x + ':' + y)
29+
return res

0 commit comments

Comments
 (0)