leetcode

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

1238.cpp (782B)


      1 class Solution {
      2   public:
      3     vector<int> circularPermutation(int n, int start) {
      4         vector<int> res = {0, 1};
      5         int idx = 2;
      6         for (int i = 1; i < n; i++) {
      7             const int size = 1 << i;
      8             for (int j = 1; j <= size; j++) {
      9                 res.push_back(size | res[size - j]);
     10                 if (res.back() == start) idx = res.size();
     11             }
     12         }
     13         if (start == 0) return res;
     14         reverse(begin(res), begin(res) + idx);
     15         reverse(begin(res) + idx, end(res));
     16         return res;
     17     }
     18 };
     19 
     20 class Solution {
     21   public:
     22     vector<int> circularPermutation(int n, int start) {
     23         vector<int> res;
     24         for (int i = 0; i < (1 << n); i++)
     25             res.push_back(start ^ i ^ (i >> 1));
     26         return res;
     27     }
     28 };