二进制手表顶部有 4 个 LED 代表 小时(0-11),底部的 6 个 LED 代表 分钟(0-59)。每个 LED 代表一个 0 或 1,最低位在右侧。
例如,下面的二进制手表读取 “3:25” 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-watch
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二进制手表,根据1的个数,求可能的时间点,
3、思路暴力两成循环,外层是小时,内层是分钟,
搞一个统计1个数的函数,
int count1(int n){ // 求这个n,中1的个数,
int res = 0;
while(n){
n = n & (n - 1);
res++;
}
return res;
}
直接暴力统计,
4、notes就暴力统计
5复杂度 6、codeclass Solution {
public:
vector readBinaryWatch(int turnedOn) {
vectorres;
for(int i = 0; i < 12; i++){
for(int j = 0; j < 60; j++){
if(count1(i) + count1(j) == turnedOn){
res.push_back( to_string(i) + ":"+
(j < 10 ? "0"+to_string(j) : to_string(j) ) ) ;
}
}
}
return res;
}
int count1(int n){ // 求这个n,中1的个数,
int res = 0;
while(n){
n = n & (n - 1);
res++;
}
return res;
}
};



