-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
61 lines (53 loc) · 1.75 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @param {number} num
* @return {string[]}
*/
var readBinaryWatch = function (num) {
function getHourCandidate (maxCount) {
const hours = [
8, 4, 2, 1, ];
function backTracking (hour, count, start, sequence, hours) {
if (hour > 11) {
return;
}
if (count === maxCount) {
return sequence.push(hour);
}
for (let i = start; i < hours.length; i++) {
backTracking(hour + hours[i], count + 1, i + 1, sequence, hours);
}
}
const sequence = [];
backTracking(0, 0, 0, sequence, hours);
return sequence;
}
function getMinuteCandidate (maxCount) {
const minutes = [
32, 16, 8, 4, 2, 1, ];
function backTracking (minute, count, start, sequence, minutes) {
if (minute > 59) {
return;
}
if (count === maxCount) {
return sequence.push(minute < 10 ? '0' + minute : minute);
}
for (let i = start; i < minutes.length; i++) {
backTracking(minute + minutes[i], count + 1, i + 1, sequence, minutes);
}
}
const sequence = [];
backTracking(0, 0, 0, sequence, minutes);
return sequence;
}
const result = [];
for (let i = 0; i < Math.min(4, num + 1); i++) {
const candidateHour = getHourCandidate(i);
const candidateMinute = getMinuteCandidate(num - i);
for (let i = 0; i < candidateHour.length; i++) {
for (let j = 0; j < candidateMinute.length; j++) {
result.push('' + candidateHour[i] + ':' + candidateMinute[j]);
}
}
}
return result;
};