-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryWatch.cpp
More file actions
96 lines (84 loc) · 2.2 KB
/
BinaryWatch.cpp
File metadata and controls
96 lines (84 loc) · 2.2 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
85
86
87
88
89
90
91
92
93
94
95
96
//A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
//
//Each LED represents a zero or one, with the least significant bit on the right.
//
//
//For example, the above binary watch reads "3:25".
//
//Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
//
//Example:
//
//Input: n = 1
//Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
//Note:
//The order of output does not matter.
//The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
//The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
#include<vector>
#include<bitset>
#include<string>
#include<iostream>
using namespace std;
class BinaryWatch
{
//public:
// vector<string> readBinaryWatch(int num) {
// vector<string> r;
// for (int i = 0; i < 12; i++)
// {
// for (int j = 0; j < 60; j++)
// {
// if (bitset<10>(i<<6|j).count()==num)
// {
// r.emplace_back(to_string(i) + (j < 10 ? ":0" : ":") + to_string(j));
// }
// }
// }
// return r;
// }
vector<int> hour = vector<int>{ 1, 2, 4, 8 }, minute = vector<int>{ 1, 2, 4, 8, 16, 32 };
public:
vector<string> readBinaryWatch(int num){
vector<string> res;
help(res, make_pair(0, 0), num, 0);
return res;
}
void help(vector<string>& r, pair<int,int> time,int num,int start){
if (!num)
{
r.emplace_back(to_string(time.first) + (time.second < 10 ? ":0" : ":") + to_string(time.second));
}
for (int j = start; j < hour.size()+minute.size(); j++)
{
if (j<hour.size())
{
time.first += hour[j];
if (time.first<12)
{
help(r, time, num - 1, j + 1);
}
time.first -= hour[j];
}
else
{
time.second += minute[j - hour.size()];
if (time.second<60)
{
help(r, time, num - 1, j + 1);
}
time.second -= minute[j - hour.size()];
}
}
}
};
void main_BinaryWatch()
{
int x=5;
BinaryWatch nb;
vector<string> r = nb.readBinaryWatch(x);
for (auto i = r.cbegin(); i != r.cend();i++)
{
cout << (*i) << endl;
}
}