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;
}
Straightforward 6line c++ solution, no need to explain


@MarkXLII said in Straightforward 6line c++ solution, no need to explain:
cktracking ha
This solution doesn't apply backtracking. It just checks every possible time and judge whether it fit the num

@mzchen said in Straightforward 6line c++ solution, no need to explain:
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
andm = 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?