-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path401.cpp
More file actions
62 lines (60 loc) · 1.78 KB
/
Copy path401.cpp
File metadata and controls
62 lines (60 loc) · 1.78 KB
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
62
class Solution {
public:
vector<string> readBinaryWatch(int num) {
vector<string> ret;
for (int i = 0; i <= num; ++i) {
int hour = i, min = num - i;
vector<int> h;
vector<int> m;
calcHour(hour, h);
vector<int> occ(6, 0);
calcMin(min, m, occ, 0, -1);
sort(h.begin(),h.end());
sort(m.begin(),m.end());
for (int j = 0; j < h.size(); ++j) {
string temp;
for (int k = 0; k < m.size(); ++k) {
if(m[k] >= 60)
continue;
temp = to_string(h[j]) + ":";
temp += (m[k] < 10 ? "0" + to_string(m[k]) : to_string(m[k]));
ret.push_back(temp);
}
}
}
return ret;
}
void calcHour(int hour, vector<int> &h) {
if (hour == 3) {
h.push_back(11);
h.push_back(7);
} else if (hour == 2) {
h.push_back(9);
h.push_back(10);
h.push_back(6);
h.push_back(5);
h.push_back(3);
} else if (hour == 1) {
h.push_back(1);
h.push_back(2);
h.push_back(4);
h.push_back(8);
} else if (hour == 0)
h.push_back(0);
}
void calcMin(int min, vector<int> &m, vector<int> &occ, int minute,int pre) {
if (min == 0) {
m.push_back(minute);
return;
}
for (int i = pre + 1; i < 6; ++i) {
if (occ[i] == 0) {
occ[i] = 1;
minute += pow(2, i);
calcMin(min - 1, m, occ, minute,i);
minute -= pow(2, i);
occ[i] = 0;
}
}
}
};