LC 969. 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 k elements 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]
Runtime: 14 ms
class Solution {
static public List<Integer> pancakeSort(int[] A) {
List<Integer> ret = new ArrayList<>();
for(int x = A.length,i=; x>= ; x--){
//System.out.print(x);
for(i=; A[i] != x; i++);
reverse(A, i);
ret.add(i+);
reverse(A, x-);
ret.add(x);
}
return ret;
}
static void reverse(int[] A, int pos){
for(int i=,j=pos; i<j; i++,j--){
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
}
LC 969. Pancake Sorting的更多相关文章
- [Solution] 969. Pancake Sorting
Difficulty: Medium Problem Given an array A, we can perform a pancake flip: We choose some positive ...
- 【leetcode】969. Pancake Sorting
题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...
- 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...
- Leetcode 969. Pancake Sorting
每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完. 时间复杂度O(n**2) class Solution(object): def pancakeSort(self, A): ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- Pancake Sorting LT969
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- 118th LeetCode Weekly Contest Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- 【LeetCode】Pancake Sorting(煎饼排序)
这道题是LeetCode里的第969道题. 题目要求: 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零 ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
随机推荐
- BootStrap【二、样式】
H5文档类型 由于使用了H5和CSS熟悉,需要在文件头引入 移动设备优先 为了对移动设备友好,需要使用标签viewport width=device-width 宽度为设备宽度 height 高度 i ...
- Python学习记录7-继承
面向对象的三大特性 封装 继承 多态 封装 封装就是对对象的成员进行访问限制 封装的三个级别: 公开,public 受保护的,protected 私有的,private public,private, ...
- Prometheus 监控 Redis 集群的正确姿势
Prometheus 监控Redis的正确姿势(redis集群) Prometheus 监控 Redis cluster,其实套路都是一样的,使用 exporter. exporter 负责采集指标, ...
- 跟着我一步一步的搭建一个基于springcloud的微服务实例
Table of Contents generated with DocToc microservicecloud 插件推荐 建立父工程Microservicecloud 搭建Employ员工服务 创 ...
- input checkbod 全选 反选
<script> var CheckBox=div.getElementsByTagName('input'); ...
- gitlab 错误处理
用gitolite新建项目,clone后首次push,可能会出现: $ git push No refs in common and none specified; doing nothing. Pe ...
- easyui tree选中指定节点,点击指定节点
功能需求描述如下: A主页面,在datagrid的某行上,操作列,点击详情,Tab页面上加载B页面 B页面,左边是树tree,右边是左边树的详情列表 要求:由A页面链接到B页面,B页面的tree,默认 ...
- bat 判断命令是否执行成功
bat 判断命令是否执行成功 连接符形式,&& 表示成功,|| 表示失败,例如: call xxx.bat && (goto succeed) || goto fail ...
- 《黑白团团队》第九次团队作业:Beta冲刺第一天
项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 团队项目Github仓库地址链接. 第一天 日期:2019/6/24 1.1 ...
- UOJ117 欧拉回路[欧拉回路]
找欧拉回路的模板题. 知识点详见图连通性学习笔记. 注意一些写法上的问题. line37&line61:因为引用,所以j和head值是同步更新的,类似于网络流的当前弧优化,除了优化枚举外,这样 ...