[leetcode] 401. Binary Watch

时间:2024-09-07 23:07:32

https://leetcode.com/contest/5/problems/binary-watch/

这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里面1的个数为满足要求的,然后判断是否超限,题目说的很清楚,小时0到11,分钟0到59,然后转化成要求的格式就行了。

 class Solution {
public:
vector<string> work(int k) {
vector<string> res;
if(k > ) return res;
for (int i = ; i < ( << ); i++) {
if(__builtin_popcount(i) == k) {
int h = i >> ;
if(h > ) continue;
int m = i & (( << ) - );
if(m > ) continue;
stringstream ss;
ss << h; ss << ":";
if(m < ) ss << "";
ss << m;
string x = ss.str();
//cout << x << endl;
res.push_back(x);
}
}
return res;
}
vector<string> readBinaryWatch(int num) {
return work(num);
}
};