leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0401.cpp (710B)


0 class Solution {
1 inline static string convert(int n) {
2 if (n < 10) return '0' + to_string(n);
3 return to_string(n);
4 }
6 public:
7 vector<string> readBinaryWatch(int turnedOn) const {
8 if (turnedOn == 0) return {"0:00"};
10 vector<string> res;
11 unsigned i = (1 << turnedOn) - 1, t;
13 while (i < (1 << 10)) {
14 const int hours = i >> 6, minutes = i & 0x3F;
16 if (hours < 12 && minutes < 60) {
17 res.push_back(to_string(hours) + ':' + convert(minutes));
18 }
20 const unsigned t = i | (i - 1);
21 i = (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(i) + 1));
22 }
24 return res;
25 }
26 };