# Straight-forward 6-line c++ solution, no need to explain

• ``````vector<string> readBinaryWatch(int num) {
vector<string> rs;
for (int h = 0; h < 12; h++)
for (int m = 0; m < 60; m++)
if (bitset<10>(h << 6 | m).count() == num)
rs.emplace_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
return rs;
}
``````

• nice solution!

• @mzchen Nice solution but where's the backtracking happening?

• cktracking ha

This solution doesn't apply backtracking. It just checks every possible time and judge whether it fit the num

• Haha, I like this idea. Actually backtracking does not necessarily work better.

• Excellent! You are so smart!

• if (bitset<10>(h << 6 | m).count() == num)

can someone please explain , what this statement doing ?

if (bitset<10>(h << 6 | m).count() == num) ?
and also why, left shifting hours 6 times ?

thanks.

• @murthi bitset is a fixed size vector that can only hold bits. It can be initialized with a number, which sets its positions to the binary values of the number. Suppose `h = 2` and `m = 3` then:

``````binary(h) = 10
binary(m) = 11
binary(h << 6) = 10000000 // shifts the value of h 6 positions to the left to make room for m
binary(h << 6 | m) = 10000011 // sets m on the first 6 bits
bitset<10>(h << 6 | m)  = [0, 0, 1, 0, 0, 0, 0, 0, 1, 1]
``````

`bitset::count()` returns how many 1s the bitset holds. The template argument on the bitset is its size.

• @Robin_Chen I've made some calculations and backtracking checks at most 252 possibilities while this brute force one always checks 708 possibilites. Why do you say backtracking does not necessarily work better?

• 卧槽 好牛逼！！！ 真心膜拜
请收下我的膝盖！！！

• so brilliant！！！！！！！！！

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