Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first kelements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.

Return the k-values corresponding to a sequence of pancake flips that sort A.  Any valid answer that sorts the array within 10 * A.lengthflips will be judged as correct.

Example 1:

Input: [3,2,4,1]
Output: [4,2,4,3]
Explanation:
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: A = [3, 2, 4, 1]
After 1st flip (k=4): A = [1, 4, 2, 3]
After 2nd flip (k=2): A = [4, 1, 2, 3]
After 3rd flip (k=4): A = [3, 2, 1, 4]
After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.

Example 2:

Input: [1,2,3]
Output: []
Explanation: The input is already sorted, so there is no need to flip anything.
Note that other answers, such as [3, 3], would also be accepted.

Note:

  1. 1 <= A.length <= 100
  2. A[i] is a permutation of [1, 2, ..., A.length]

对于这种题倒着处理。

主要是看两种情况,

3 1 2

要把2放后面,这1 2 3变成2 1 3,再全部倒着来一次3 1 2就OK

2 3 1这种,就直接全部倒着来就行。

class Solution {
public:
int arr[];
void reverse1(int from,int to) {
while(from<to){
int tmp=arr[from];
arr[from++]=arr[to];
arr[to--]=tmp;
}
}
int Find(int num,int last){
for(int i=;i<last;i++){
if(arr[i]==num){
return i+;
}
}
return ;
}
void x(int len){
for(int i=;i<len;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
} vector<int> pancakeSort(vector<int>& A) {
vector<int>Ve = A;
vector<int>Vee;
sort(Ve.begin(),Ve.end()); int Size = A.size(); for(int i=;i<Size;i++){
arr[i] = Ve[i];
} for(int i=;i<Size;i++){
int address = Find(A[Size - i - ],Size-i);
if(address == ){
//cout<<address<<" A"<<endl;
reverse1(,Size - i - );
//x(Size);
if(Size - i == ){
continue;
}
Vee.push_back(Size - i); }else if(address == Size - i){
//cout<<address<<" B "<<Size - i<<endl;
continue;
}else{
//cout<<address<<" C"<<endl;
reverse1(,address - );
//x(Size);
if(address == ){
continue;
}
Vee.push_back(address);
reverse1(,Size - i - );
//x(Size);
if(Size - i == ){
continue;
}
Vee.push_back(Size - i);
}
}
reverse(Vee.begin(),Vee.end());
return Vee;
}
};

118th LeetCode Weekly Contest Pancake Sorting的更多相关文章

  1. 118th LeetCode Weekly Contest Powerful Integers

    Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some ...

  2. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 118

    要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given tw ...

  8. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  9. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

随机推荐

  1. SpringAOP02 自定义注解

    1 自定义注解 1.1 创建自定义注解 从java5开始就可以利用 @interface 来定义自定义注解 技巧01:注解不能直接干扰程序代码的运行(即:注解的增加和删除操作后,代码都可以正常运行) ...

  2. Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的

    Spring框架找不到 applicationContext.xml文件,可能是由于applicationContext.xml文件的路径没有放在根目录下造成的

  3. 507. Perfect Number 因数求和

    [抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...

  4. 19、SOAP安装,运用与比对结果解释

    转载:http://www.dengfeilong.com/post/Soap2.html https://blog.csdn.net/zhu_si_tao/article/details/71108 ...

  5. Luogu 1099 树网的核

    bzoj1999 数据加强版(n <= 5e5) 较早的noip题,值得研究 重要结论:直径的最长性,任何从直径中离开直径的点到它离开的点的距离,都不会比直径的另一端到它离开的点长(否则就有新的 ...

  6. TreeView的绑定与读取

    /// <summary>        /// 绑定TreeView        /// </summary>        public void BindTreeVie ...

  7. Word文件乱码XML

    文章介绍 一个朋友写的文档因为异常关机,导致全部文件变成了xml的乱码,正好帮他解决了,感觉这些或许有些帮助,就先记录下来了. 破损文件介绍 文件破坏之后,打开全是xml格式的文档,结构如下. 恢复过 ...

  8. RegExp正则表达式对象

    JavaScript的RegExp对象有两种创建方式,一种是字面量,一种是对象. var r = /pattern/attributes或者new RegExp(pattern, attributes ...

  9. 【转】Android自定义控件(二)——有弹性的ScrollView

    原文地址:http://blog.csdn.net/a105865708/article/details/17784041 实现了当手指滑动到ScrollView的顶部.底部时, 可以继续的向上.向下 ...

  10. 模态显示PresentModalViewController

    1.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...