解题思路:
本题要求,根据输入的二进制亮灯的数量,来求解出所有可能得时间,我们知道时间的显示是在一个固定的范围,所以可以考虑枚举求解,然后按照条件进行筛选
注意;
1.枚举的范围
2.string的处理
class Solution {
public:
// 暴力破解枚举法
vector<string> readBinaryWatch(int turnedOn) {
vector<string> res;
for(int h = 0;h<12;++h){
for(int m = 0;m < 60;++m){
//__buitin_popcount是GCC的内建函数,它可以精确的计算二进制中1的个数。
if(__builtin_popcount(h) + __builtin_popcount(m) == turnedOn){
res.push_back(to_string(h) + ":" + (m<10?"0":"") + to_string(m));
}
}
}
return res;
}
};