解题思路:
作为简单题的唯一可能就是用枚举法去解决了,枚举所有的小时和分钟,检测二进制中1的个数相加是否等于turnedOn,等于则组合成符合要求的时间规格,代码如下:
class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
vector<string> res;
// 枚举所有的小时和分钟
for(int i = 0; i < 12; i ++) {
for(int j = 0; j < 60;j ++) {
// 检测二进制中1的个数相加是否等于turnedOn
if(__builtin_popcount(i) + __builtin_popcount(j) == turnedOn) {
string s = to_string(i) + ":" + (j >= 10 ? to_string(j) : ("0" + to_string(j)));
res.push_back(s);
}
}
}
return res;
}
};
/*作者:heroding
链接:https://leetcode-cn.com/problems/binary-watch/solution/cmei-ju-fa-by-heroding-r47n/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/