-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcde-401(back-tracking).cpp
32 lines (29 loc) · 1.09 KB
/
leetcde-401(back-tracking).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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> hour = {1, 2, 4, 8}, minute = {1, 2, 4, 8, 16, 32};
void helper(vector<string>& res, pair<int, int> time, int num, int start_point) {
if (num == 0) {
res.push_back(to_string(time.first) + (time.second < 10 ? ":0" : ":") + to_string(time.second));
return;
}
for (int i = start_point; i < hour.size() + minute.size(); i ++)
if (i < hour.size()) {
time.first += hour[i];
if (time.first < 12) helper(res, time, num - 1, i + 1); // "hour" should be less than 12.
time.first -= hour[i];
} else {
time.second += minute[i - hour.size()];
if (time.second < 60) helper(res, time, num - 1, i + 1); // "minute" should be less than 60.
time.second -= minute[i - hour.size()];
}
}
int main()
{
vector<string> res;
helper(res,make_pair(0,0),3,0);
for(auto s:res)
cout<<s<<endl;
return 0;
}