https://leetcode.com/contest/5/problems/binary-watch/
这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里面1的个数为满足要求的,然后判断是否超限,题目说的很清楚,小时0到11,分钟0到59,然后转化成要求的格式就行了。
1 class Solution { 2 public: 3 vector<string> work(int k) { 4 vector<string> res; 5 if(k > 10) return res; 6 for (int i = 0; i < (1 << 10); i++) { 7 if(__builtin_popcount(i) == k) { 8 int h = i >> 6; 9 if(h > 11) continue; 10 int m = i & ((1 << 6) - 1); 11 if(m > 59) continue; 12 stringstream ss; 13 ss << h; ss << ":"; 14 if(m < 10) ss << "0"; 15 ss << m; 16 string x = ss.str(); 17 //cout << x << endl; 18 res.push_back(x); 19 } 20 } 21 return res; 22 } 23 vector<string> readBinaryWatch(int num) { 24 return work(num); 25 } 26 };