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. 微信内置浏览器中的cookie很诡异呀

    微信内置浏览器中的cookie很诡异呀 这是设置和删除COOKIE的代码 function set_cookie($var ,$value = '' ,$expire = 0){ $path = '/ ...

  2. 383. Ransom Note 在字典数组中查找笔记数组

    [抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...

  3. Java c3p0 连接 MySQL

    <?xml version="1.0" encoding="UTF-8"?> <!-- 需要导入c3p0驱动jar包和mysql驱动jar包 ...

  4. osm2pgsql导入少字段

    Explanation: osm2pgsql imports normally the data in a static database schema. The tags without a cor ...

  5. jq获取table总行数

    var rows = $('table').find("tr").length;

  6. 函数有一个特殊的属性 prototype!

    JavaScript 中只有对象,没有类. 实际上,JavaScript 才是真正应该被称为「面向对象」的语言,因为它是少有的可以不通过类,直接创建对象的语言. 函数的 prototype 属性 在 ...

  7. [GO]局部变量的特点

    package main import "fmt" func main() { //定义在{}里的变量就是局部变量,只能在{}里起作用 //作用域,变量起作用的范围 //执行到定义 ...

  8. Densely Connected Convolutional Networks(緊密相連卷積網絡)

    - Dense blocks where each layer is connected to every other layer in feedforward fashion(緊密塊是指每一個層與每 ...

  9. 安装vmtools Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl.

    Error: Unable to execute "/usr/bin/vmware-uninstall-tools.pl. 安装vmware tools错误解决办法 很多朋友都在用vmwar ...

  10. <%@ include > 与< jsp:include >

    include指令表示在JSP编译时插入一个包含文本或者代码的文件,把文件中的文本静态地包含过去.也就是说,会把被包含的页面拷贝到包含的页面中指令所在的位置. 语法格式:<%@ include ...