-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path401_binaryWatch.cpp
More file actions
84 lines (78 loc) · 1.39 KB
/
Copy path401_binaryWatch.cpp
File metadata and controls
84 lines (78 loc) · 1.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int powerOf2(int x)
{
int res = 1;
for (size_t i=0;i<x; ++i)
{
res*=2;
}
return res;
}
void getValue(int h, int start, vector<bool>& bits, vector<int>& res, int maxValue)
{
if (h==0)
{
int r = 0;
for(size_t i=0; i<bits.size(); ++i)
{
if (bits[i])
r+=powerOf2(i);
}
if (r<maxValue)
{
res.push_back(r);
}
}
else {
for (size_t i=start; i<bits.size(); ++i)
{
if (!bits[i])
{
bits[i] = true;
getValue(h-1, i+1, bits, res, maxValue);
bits[i] = false;
}
}
}
}
void buildTime(int h, int m, vector<string>& res)
{
vector<int> hours;
vector<bool> hBits(4, false);
getValue(h, 0, hBits, hours, 12);
vector<int> minutes;
vector<bool> mBits(6, false);
getValue(m, 0, mBits, minutes, 60);
for (auto hour:hours)
{
for (auto min:minutes)
{
string curr = to_string(hour)+":"+( min<10?"0":"")+to_string(min);
res.push_back(curr);
}
}
}
vector<string> readBinaryWatch(int num) {
vector<string> res;
for (size_t i=0;i<=min(num, 4);++i)
{
if (num-i>=6)
continue;
buildTime(i, num-i, res);
}
return res;
}
};
int main()
{
Solution solution;
vector<string> res = solution.readBinaryWatch(2);
for(auto& str:res)
cout << str << endl;
return 0;
}