forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (36 loc) · 918 Bytes
/
main.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
/// Source : https://leetcode.com/problems/binary-watch/
/// Author : liuyubobobo
/// Time : 2020-02-15
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
/// Brute Force
/// Time Complexity: O(2^10)
/// Space Complexity: O(1)
class Solution {
public:
vector<string> readBinaryWatch(int num) {
vector<string> res;
for(int i = 0; i < (1 << 10); i ++)
if(num_of_ones(i) == num){
int m = i & 0b111111, h = i >> 6;
if(h < 12 && m < 60){
stringstream ss;
ss << h << ":" << setfill('0') << setw(2) << m;
res.push_back(ss.str());
}
}
return res;
}
private:
int num_of_ones(int x){
int res = 0;
while(x) res += x % 2, x /= 2;
return res;
}
};
int main() {
return 0;
}