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)


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