# 0ms C++ code with explanation!

• You can use vectors to store all the conditions! For example, if only 2 hour leds are on>! Spoiler, the hour must be one of {3,5,6,9,10}. Because 3,5,6,9,10 all have and only have two "1" in their binary notation. Then, that's ok! A for-for loop will print all the possible condition!
Note:
You can automatically push different hours and minutes in your corresponding vector.
There is an efficient way to calculate how many "1" in a integer.
For example,
a = 0bXXX10000;
a - 1 = 0bXXX01111;
a & (a - 1) = 0bXXX00000;
X represent 0 or 1. Then a & (a - 1) would make kill the far right "1".
So, you only need calculate n times which equals the number of "1" in a integer.

``````vector<string> readBinaryWatch(int num) {
vector<string> hour[5];
vector<string> min[7];
vector<string> tr;
for(int i = 0; i < 12; i++)
{
int numofone = getNumOfOne(i);
hour[numofone].push_back(to_string(i));
}
for(int i = 0; i < 60; i++)
{
int numofone = getNumOfOne(i);
min[numofone].push_back(to_string(i));
}
for(int i = 0; i <= num; i++)
{
if(i >= 0 && num - i >= 0 && i < 5 && num - i < 7)
{
for(int j = 0; j < hour[i].size(); j++)
{
for(int k = 0; k < min[num - i].size(); k++)
{
tr.push_back(hour[i].at(j) + ":" + (min[num - i].at(k).size() <= 1 ? "0" : "") + min[num - i].at(k));
}
}
}
}
return tr;
}
int getNumOfOne(int x)
{
int num = 0;
while(x)
{
x &= (x - 1);
num++;
}
return num;
}
``````

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.