-
Notifications
You must be signed in to change notification settings - Fork 28
/
Binary_Watch.cpp
58 lines (55 loc) · 1.85 KB
/
Binary_Watch.cpp
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
// backtracking; output limit exceeded
class Solution {
void readBinaryWatch(int currHour, int currMin, int n, int indx1, vector<int>& hours, int indx2, vector<int>& minutes, vector<string>& result) {
if(n == 0) {
string solution = to_string(currHour);
char minBuffer[2];
sprintf(minBuffer, "%02d", currMin);
solution += ":";
solution += string(minBuffer);
result.push_back(solution);
return;
}
if(indx2 >= minutes.size()) {
return;
}
for(int i = indx1; i < hours.size(); ++i) {
if(currHour + hours[i] >= 12) {
break;
}
readBinaryWatch(currHour + hours[i], currMin, n - 1, indx1 + 1, hours, indx2, minutes, result);
}
for(int i = indx2; i < minutes.size(); ++i) {
if(currMin + minutes[i] >= 60) {
break;
}
readBinaryWatch(currHour, currMin + minutes[i], n - 1, indx1, hours, indx2 + 1, minutes, result);
}
}
public:
vector<string> readBinaryWatch(int num) {
vector<int> hours {1, 2, 4, 8};
vector<int> minutes {1, 2, 4, 8, 16, 32};
vector<string> result;
if(num > hours.size() + minutes.size()) {
return result;
}
readBinaryWatch(0, 0, num, 0, hours, 0, minutes, result);
return result;
}
};
// bit manipulation; AC
class Solution {
public:
vector<string> readBinaryWatch(int num) {
vector<string> result;
for (int h = 0; h < 12; h++) {
for (int m = 0; m < 60; m++) {
if (bitset<10>(h << 6 | m).count() == num) {
result.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
}
}
}
return result;
}
};