Skip to content

Commit 0997ae0

Browse files
committed
Add 969-PancakeSorting.cpp
1 parent 066a216 commit 0997ae0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> pancakeSort(vector<int>& arr) {
4+
int n = arr.size();
5+
std::vector<int> b(arr);
6+
sort(b.rbegin(), b.rend());
7+
8+
std::vector<int> res;
9+
for(int p = 0; p < n; p++){
10+
int idx(-1);
11+
for(int u = 0; idx < 0 && u < n; u++){if(arr[u] == b[p]){idx = u;}}
12+
res.push_back(idx + 1); std::reverse(arr.begin(), arr.begin() + idx + 1);
13+
res.push_back(n - p); std::reverse(arr.begin(), arr.begin() + n - p);
14+
}
15+
16+
return res;
17+
}
18+
};

0 commit comments

Comments
 (0)