class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
vector<string> ans;
for(int h=0;h<12;h++){ //小时
for(int m=0;m<60;m++){ //分钟
//2^6=64>59,故h<<6,这样h和m互不影响,通过|得到⼆进制位中1的个数
bitset<10> bst(h<<6|m);
if(bst.count()==turnedOn){ //bst.count()得到bst中⼆进制位1的个数
string t=to_string(h)+":";
if(m<10) t+="0";
t+=to_string(m);
ans.push_back(t);
}
}
}
return ans;
}
};
LeetCode-401. 二进制手表-easy
最新推荐文章于 2024-05-21 23:16:53 发布