118th LeetCode Weekly Contest Pancake Sorting
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.length
flips 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 <= A.length <= 100
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的更多相关文章
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 118
要死要死,第一题竟然错误8次,心态崩了呀,自己没有考虑清楚,STL用的也不是很熟,一直犯错. 第二题也是在室友的帮助下完成的,心态崩了. 970. Powerful Integers Given tw ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
随机推荐
- 面试题:基础数据类型 包装类 int Integer
因为在学习集合时知道集合里存放的对象都是Object类型,取出的时候需要强制类型转换为目标类型(使用泛型集合不需要),如int a = (Integer)arrayList.get(0):然后我们就会 ...
- linux环境配置与使用合集
配置linux和samba共享 1. 安装linux操作系统 2. 通过windows操作系统ping linux看看是否可以ping通 3. 相关软件安装 a. 安装samba sudo apt-g ...
- 数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
数据库MySQL之 视图.触发器.存储过程.函数.事务.数据库锁.数据库备份.事件 浏览目录 视图 触发器 存储过程 函数 事务 数据库锁 数据库备份 事件 一.视图 1.视图概念 视图是一个虚拟表, ...
- rpm遇到的坑-与VMP冲突
rpm遇到的坑-与VMP冲突 摘自:https://blog.csdn.net/shijichao2/article/details/78797586 2017年12月13日 22:29:21 阅读数 ...
- 安装sql server 2008重启失败
sql server2008安装时提示重启计算机失败怎么办 安装SQL Server 2008时,经常会遇到这样一个问题,软件提示“重启计算机失败”,如果忽略的话,会给后面的安装带来很大的麻烦,这 ...
- Python开发 第一篇 python的前世今生
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- [GO]随机生成四们数字
package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) ...
- plsql中的执行体
在plsql中的sql windows窗口中,可以编写一段执行体来达到一定的目的,类似于写一段程序,可有逻辑判断. 大概的格式为 declare ----定义变量 begin ----- 执行体: e ...
- struts2 、mybatis 、easyui 分页
rows page 控件自动提交这两个参数 pageSize number The page size. 10pageNumber number Show the page number when p ...
- Socket编程(c语言示例)
转自:http://blog.csdn.net/dxpqxb/article/details/8166423 前言 Socket可以看成在两个程序进行通讯连接中的一个端点,是连接应用程序和网络驱动程序 ...