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. WDCP从php5.2升级到5.3的办法,以及升级过程中iconv错误的处理

    从wdcp官方论坛我们可以找到一个询问升级的帖子,然后管理员在回复中也提供了升级方法: cd /tmp wget -c http://dl.wdlinux.cn:5180/soft/php-5.3.1 ...

  2. 22-Two(公共子序列的个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. lucene和solr

    我们为什么要用solr呢? 1.solr已经将整个索引操作功能封装好了的搜索引擎系统(企业级搜索引擎产品) 2.solr可以部署到单独的服务器上(WEB服务),它可以提供服务,我们的业务系统就只要发送 ...

  4. java就业指南 zookeeper分布式系统 zookeeper实现分布式锁 有用

    目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个 分布式系统都无法同时满足一致性(Consistency).可用性 ...

  5. Linux 与 BSD

    1)Linux 与 BSD 有什么不同? http://linux.cn/article-3186-1.html 2)BSD(Unix)家族 http://blog.csdn.net/cradmin/ ...

  6. pip指定安装包版本

    pip是目前最好用的python包关系工具,没有之一. 1.版本范围符号:==.>=.<=.>.<. 2.单个安装示例: 1 2 3 pip install cchardet  ...

  7. python之yield函数

    yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist( ...

  8. 如何在Django模型中管理并发性 orm select_for_update

    如何在Django模型中管理并发性 为单用户服务的桌面系统的日子已经过去了 - 网络应用程序现在正在为数百万用户提供服务,许多用户出现了广泛的新问题 - 并发问题. 在本文中,我将介绍在Django模 ...

  9. 前端基础 之 Bootstrap框架

    浏览目录 Bootstrap介绍 为什么要使用Bootstrap? Bootstrap环境搭建 布局容器 栅格系统 Bootstrap全局样式 一.Bootstrap介绍 Bootstrap是Twit ...

  10. 1.python系统性能信息模块之psutil模块

    Psutil(进程和系统实用程序)是一个跨平台的库,用于在Python中检索有关运行进程和系统资源利用率(CPU,内存,磁盘,网络)的信息.它主要用于系统监视,分析和限制系统资源及运行进程的管理.它实 ...