problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::pancake_sorting::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< int > pancakeSort (vector< int > &arr)
 

详细描述

在文件 leetcode.h1194 行定义.

成员函数说明

◆ pancakeSort()

vector< int > leetcode::pancake_sorting::Solution::pancakeSort ( vector< int > &  arr)
static

在文件 leetcode.cpp3064 行定义.

3064 {
3065 auto ans = vector<int>();
3066 const int n = arr.size();
3067 auto *current = new int[n];
3068 auto *sorted = new int[n];
3069 for(int i = 0; i < n; i++) {
3070 current[i] = arr[i];
3071 sorted[i] = arr[i];
3072 }
3073 sort(sorted, sorted + n);
3074 RESTART:
3075 for(int i = n - 1; i >= 0; i--) {
3076 if(current[i] != sorted[i]) {
3077 const int target = sorted[i];
3078 int target_i = -1;
3079 for(int j = 0; j <= i; j++) {
3080 if(current[j] == target) {
3081 target_i = j + 1;
3082 break;
3083 }
3084 }
3085 if(target_i == 1) {
3086 target_i = i + 1;
3087 }
3088 ans.push_back(target_i);
3089 for(int j = 0; j < target_i / 2; j++) {
3090 swap(current[j], current[target_i - j - 1]);
3091 }
3092 goto RESTART;
3093 }
3094 }
3095 delete[] current;
3096 delete[] sorted;
3097 return ans;
3098 }

该类的文档由以下文件生成: