Posted onEdited onInLeetCodeDisqus: Symbols count in article: 447Reading time ≈1 mins.
O(n2) time O(1) space 因为题目要求任何解法都可以,所以用最直白的思路,先从大到小找到每一个数,然后把这个数翻转到最开始,再翻转到对应的位置即可 类似bubble sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: vector<int> pancakeSort(vector<int>& A){ int n = A.size(); vector<int> res; for (int i = n - 1; i >= 0; --i) { int k = distance(begin(A), find(begin(A), begin(A) + i + 1, i + 1)); res.push_back(k + 1); reverse(begin(A), begin(A) + k + 1); res.push_back(i + 1); reverse(begin(A), begin(A) + i + 1); } return res; } };