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 <= 100A[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 ...
随机推荐
- IDM下载百度资源出现403的解决方法
测试发现是受cookie的影响,百度为了防止用外部下载工具突破限速加入了cookie验证,因为一般的下载工具请求下载的时候不会附加cookie信息. IDM就是这样,它请求下载文件时只知道文件的下载地 ...
- Spring Boot Start 打包方式装B指南
项目结构如下: test包:实际的代码 spring-boot-start-test包:start 配置包 代码详细配置如下 https://github.com/fqybzhangji/spring ...
- vmware添加新硬盘磁盘扫描脚本
#! /bin/bash echo "- - -" > /sys/class/scsi_host/host0/scan echo "- - -" > ...
- BZOJ5017 [Snoi2017]炸弹[线段树优化建边+scc缩点+DAG上DP/线性递推]
方法一: 朴素思路:果断建图,每次二分出一个区间然后要向这个区间每个点连有向边,然后一个环的话是可以互相引爆的,缩点之后就是一个DAG,求每个点出发有多少可达点. 然后注意两个问题: 上述建边显然$n ...
- object xml
http://stackoverflow.com/questions/17739330/xmlserializer-convert-c-sharp-object-to-xml-string http: ...
- pandas.DataFrame.where和mask 解读
1.前言背景 没怎么用过df.where 都是直接使用loc.apply等方法去解决. 可能是某些功能还没有超出loc和apply的适用范围. 2.进入df.where和df.mask DataFra ...
- Bootstrap-轮播图-No.5
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Bootstrap-Bootstrap官网卡片响应式布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- springboot2.0入门(一)----springboot 简介
一.springboot解决了什么? 避免了繁杂的xml配置,框架自动帮我们完成了相关的配置,当我们需要进行相关插件集成的时候,只需要将相关的starter通过相关的maven依赖引进,并可以进行相关 ...
- vue项目实现详情页后退缓存之前的数据
vue项目实现详情页后退缓存之前的数据 2019年02月19日 14:54:57 不想写代码的程序员 阅读数:244 一.需要缓存的内容: 1.后退缓存条件查询的数据 2.后退缓存分页信息 二.实 ...