leetcode969
class Solution(object):
def pancakeSort(self, A: 'List[int]') -> 'List[int]':
n = len(A)
result = list()
while n > 1:
idx = A.index(n)
dif = n - 1 - idx
pre = []
if dif != 0:
if idx != 0:
result.append(idx+1)
result.append(n)
pre = A[idx+1:]
pre.reverse()
A = pre + A[:idx]
n = len(A)
#print(A)
return result
从大到小寻找每个数字的位置,将其先移动到第一个位置,再将其移动到其数字应该的位置(index==num-1)。
leetcode969的更多相关文章
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- LeetCode969. 煎饼排序
问题:969. 煎饼排序 给定数组 A,我们可以对其进行煎饼翻转:我们选择一些正整数 k <= A.length,然后反转 A 的前 k 个元素的顺序.我们要执行零次或多次煎饼翻转(按顺序一次接 ...
随机推荐
- c语言题库---- 函数
---恢复内容开始--- 1.编写一个函数,功能为返回两个int类型参数的最大的值 #include <stdio.h>int FindMax( int a, int b); int ma ...
- 使用 Flask-Docs 自动生成 Api 文档
影响我写文档的原因可能是代码和文档分离,有时候写完代码会忘记补文档,而且不能及时查看,使用 Flask-Docs 可以解决我的问题,这个插件可以根据代码注释生成文档页面,代码注释改动文档可以及时更新, ...
- 3-Longest Substring Without Repeating Characters @LeetCode
3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...
- 动画讲解TCP的3次握手,4次挥手
https://mp.weixin.qq.com/s/TUBhH_lJe6M4KgAZO-rP2A TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入 ...
- 目标文件obj的各段 2
#程序的自我修养 page68
- Centos 6.9 install Python3.7
# install python3sudo yum -y updatesudo yum -y install yum-utils yum install -y zlib-devel bzip2-dev ...
- VS2015Git 源代码工具使用
1. 首先到源代码托管平台申请个账户:https://git.oschina.net/ 2.创建流程图: 2.1 开始创建项目: 2.2 3. 4. 作者:江宁织造 qq空间:苦心孤诣博客:http: ...
- [UE4]Spline使用注意事项
一.如果在Character中使用Spline,则使用Add Spline Mesh Componet的时候,要注意设置Transform.Mobility为Movable 二.使用Predict P ...
- aspose.cells 复制单元格
将第1行至第27行复制到第28行 cells.CopyRows(cells, 0, i*27, 27);
- mysql通过now()获取的时间不对
先用now()获取系统时间,发现时间不对(差8个小时): mysql> select now(); +---------------------+ | now() | +------------ ...