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.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 <= A.length <= 100A[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/ [题意] 告诉你任意两个 ...
随机推荐
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- spring框架 事务 注解配置方式
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...
- R dataframe 遗忘, which 矩阵搜索
A data frame is used for storing data tables. It is a list of vectors of equal length. For example, ...
- EZOJ #202
传送门 分析 我们知道选一个点的代价就是他所有出边边权的异或和 由于一条边如果两个端点均选边权会异或两次变回0,所以不必担心重复的情况 于是直接跑线性基即可 代码 #include<bits/s ...
- 权限管理RBAC
四张表: 1.module:id/name //模块 2.action:id /module_id/name //权限 3.user:id/name //用户表 4.group:id/user_id ...
- 用JS实现点击TreeView根节点复选框全选
以下两种方法哪个不报错就用哪个.用法都是在TreeView标签中加入OnClick="",然后引入函数名即可 第一种方法:(摘自:http://www.cnblogs.com/fr ...
- sql2008调试存储过程
拿上篇存储过程为例: 在意个窗口里面写上exec Proc_MoveUpOrDown2 'id',3,1,'tableName,'orderid' 按F11,有个黄色的箭头会指向该行, 再按F11会跳 ...
- TensorFlow中文手册
注意:本文只为读书笔记. 第一章 起步 - 起步 - [介绍](SOURCE/get_started/introduction.md) - [下载及安装](SOURCE/get_started/os_ ...
- 编写高质量代码改善C#程序的157个建议——建议48:Dispose方法应允许被多次调用
建议48:Dispose方法应允许被多次调用 一个类型的Dispose方法应该允许被多次调用而不抛出异常.鉴于此,类型内部维护了一个私有的bool变量disposed,如下: private bool ...
- eclipse中的项目无法在build/classes目录下生成.class字节码
转载 原文链接:https://www.cnblogs.com/iceblow/p/6648715.html 1.首先确定project->Build Automatically是否勾选上: ...