leetcode

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

1104.cpp (593B)


      1 #define LOG2(X) ((unsigned)(8 * sizeof(int) - __builtin_clz((X)) - 1))
      2 
      3 class Solution {
      4     int flip(int label) {
      5         int log = LOG2(label), floor = 1 << log, ceil = (1 << log + 1) - 1;
      6         return floor + ceil - label;
      7     }
      8 
      9   public:
     10     vector<int> pathInZigZagTree(int label) {
     11         bool rev = LOG2(label) % 2;
     12         vector<int> res({label});
     13         if (rev) label = flip(label);
     14         while ((label /= 2) > 0) {
     15             rev = !rev;
     16             res.push_back(!rev ? label : flip(label));
     17         }
     18         reverse(res.begin(), res.end());
     19         return res;
     20     }
     21 };