每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完.

时间复杂度O(n**2)

class Solution(object):
def pancakeSort(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
maxA,index,ret,size = 0,-1,[],len(A)
if size==1: return [] for i, val in enumerate(A):
if val > maxA:
index,maxA = i,val A = A[index::-1] + ([] if index == size - 1 else A[index + 1:])
A.reverse()
ret = ret + [index + 1, size]+self.pancakeSort(A[:size - 1]) return ret

Leetcode 969. Pancake Sorting的更多相关文章

  1. 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...

  2. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  3. 【LeetCode】Pancake Sorting(煎饼排序)

    这道题是LeetCode里的第969道题. 题目要求: 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零 ...

  4. [Solution] 969. Pancake Sorting

    Difficulty: Medium Problem Given an array A, we can perform a pancake flip: We choose some positive ...

  5. LC 969. Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  6. 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 ...

  7. [Swift]LeetCode969.煎饼排序 | Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  8. Pancake Sorting LT969

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  9. Leetcode 969. 煎饼排序

    969. 煎饼排序  显示英文描述 我的提交返回竞赛   用户通过次数134 用户尝试次数158 通过次数135 提交次数256 题目难度Medium 给定数组 A,我们可以对其进行煎饼翻转:我们选择 ...

随机推荐

  1. django路由系统之反向生成url

    from niubin.service import v1 from django.urls import reverse from django.shortcuts import HttpRespo ...

  2. C语言预处理命令的使用

    cppreference.com -> 预处理命令 -> 详细说明 预处理命令 #,## # 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符 ...

  3. layer满屏/禁止最大化最小化 可以做选择框使用

    1.layer弹窗最大化 var index=layer.open(); layer.full(index); 2.layer禁止最大化最小化 layer.open( [ type:2, title: ...

  4. <script>放在head内和body内有什么区别

    加载的顺序不一样,你可以把HTML看成从上往下加载的. 例如在网速慢的情况下把js代码放在body底部用户会先看到网页结构,等js加载完成后才出现特效 区别简述: 在HTML body部分中的Java ...

  5. python中初始化实例属性

    虽然我们可以自由地给一个实例绑定各种属性,但是,现实世界中,一种类型的实例应该拥有相同名字的属性.例如,Person类应该在创建的时候就拥有 name.gender 和 birth 属性,怎么办? 在 ...

  6. 常用模块---sys&logging&序列化模块(json&pickle)

    sys 模块 sys.argv 命令行参数List,第一个元素是程序本身路径,通常用来避免io 阻塞 print('欢迎进入') info=sys.argv ': print('login succe ...

  7. DNS 安装配置

    DNS 安装配置 实验环境 一台主机:Linux Centos 6.5 32位 安装包: DNS服务:bind.i686 DNS测试工具:bind-utils DNS 服务安装 1.yum安装DNS服 ...

  8. jQuery自动轮播图片焦点图

    在线演示 本地下载

  9. codeforces上某题

    一道codeforces上的题目. 题目大意: 定义有k个不同的字符的字符串为好字符串.现在给出一个字符串,求解对该字符串的每个前缀Si至少是多少个好字符串的连接,若不能由好字符串连接而成则输出-1. ...

  10. class_exists — 检查类是否已定义

    class_exists — 检查类是否已定义 bool class_exists ( string $class_name [, bool $autoload = true ] ) 检查指定的类是否 ...