forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0401.cpp
40 lines (39 loc) · 940 Bytes
/
0401.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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<string> readBinaryWatch(int num)
{
vector<string> result;
for (unsigned int h = 0; h < 12; ++h)
{
for (unsigned int m = 0; m < 60; ++m)
{
if ((countOnes(h) + countOnes(m)) == num)
{
stringstream ss;
ss << h << ":" << setfill('0') << setw(2) << m;
result.push_back(ss.str());
}
}
}
return result;
}
private:
unsigned int countOnes(int num)
{
unsigned int count = 0;
while (num != 0)
{
num = num & (num - 1);
count++;
}
return count;
}
};