leetcode

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

0969.cpp (604B)


      1 class Solution {
      2   public:
      3     vector<int> pancakeSort(vector<int> &arr) {
      4         vector<int> res;
      5 
      6         for (int i = arr.size() - 1, low, high; i >= 0; i--) {
      7             if (arr[i] == i + 1) continue;
      8 
      9             low = 0, high = 0;
     10             while (arr[high] != i + 1)
     11                 high++;
     12 
     13             res.push_back(high + 1);
     14             while (low < high)
     15                 swap(arr[low++], arr[high--]);
     16 
     17             low = 0, high = i;
     18             res.push_back(high + 1);
     19             while (low < high)
     20                 swap(arr[low++], arr[high--]);
     21         }
     22         return res;
     23     }
     24 };