【leetcode】969. Pancake Sorting
题目如下:
Given an array
A
, we can perform a pancake flip: We choose some positive integerk <= A.length
, then reverse the order of the first k elements ofA
. We want to perform zero or more pancake flips (doing them one after another in succession) to sort the arrayA
.Return the k-values corresponding to a sequence of pancake flips that sort
A
. Any valid answer that sorts the array within10 * 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]
解题思路:本题没有要求求出最少的操作次数,而且规定了操作上限是 10 * A.length。我的方法是首先把最大的数移到最后,这里只需要两步,第一是找出最大的数的位置,并且把这一段反转,这样的话最大数就在第一位了,接下来逆转整个数组,最大数就被翻转到最后。接下来是次大数,方法也一样,只不过在操作的过程中不处理数组最后一个元素(即已经放好位置的最大数)。这种方法理论上的最大操作次数是 2 * A.length。
代码如下:
class Solution(object):
def pancakeSort(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
bound = len(A)
sa = sorted(A)
end = len(A)
res = []
while A != sa:
inx = A.index(bound)
if inx != 0:
A = A[0:inx+1][::-1] + A[inx+1:]
res.append(inx+1)
A = A[:end][::-1] + A[end:]
res.append(end)
end -= 1
bound -= 1
return res
【leetcode】969. Pancake Sorting的更多相关文章
- 【LeetCode】969. Pancake Sorting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟法 日期 题目地址:https://leetco ...
- 【Leetcode】Sort List (Sorting)
这个问题需要与归并排序排两个名单,基本思路分为切割与合并 合并后的代码Merge Two Sorted List里已经讲得非常清楚了. 所以这里直接给出代码. public ListNode merg ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- jquery 选项卡切换
html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- 【串线篇】面向切面编程AOP
面向切面编程AOP 描述:将某段代码“动态”的切入到“指定方法”的“指定位置”进行运行的一种编程方式 (其底层就是Java的动态代理)spring对其做了简化书写 场景: 1).AOP加日志保存到数据 ...
- 用List和Map排序输出
参考:java的treemap反序输出 int->string string->int java对象数组的概述 List import java.io.*; import java.uti ...
- 流量隔离方案 Dpath 护航双十一新零售
需求 在今年的双11准备期间,业务同学提出要针对新零售进行特殊的保障,希望新零售过来的流量,单独进入到一批机器,和其他普通流量隔离开来,这对新零售系统稳定性提出更高的要求. 需求总结下来就是: 针对特 ...
- 怎么让小白理解intel处理器(CPU)的分类
https://www.zhihu.com/question/32669957 目录 如何选购台式机CPU? 1. 英特尔处理器简介(本文) 1.1 聊聊Intel Tick-Tock 2. AMD处 ...
- CF 1045 H. Self-exploration 解题报告
CF 1045 H. Self-exploration 考虑到串的结构一定是 1...0....1....0.....1... 这样的,而\(01\)与\(10\)在转折点交替出现 首先串长一定是\( ...
- oracle聚合函数avg()注意点
avg:用avg函数进行平均运算时会忽略空值(即最终出现的平均值不对[如果原始数据中存在空值的话]),可以这样子来解决:avg(nvl(comm,0))或者sum(comm)/count(*)---- ...
- linux基础知识汇总(三)-vmware下ubuntu上网配置
方式1 : 使用NAT共享IP的方式.使用这种方式什么都不用设置就可以在ubuntu中上网冲浪了. 备注: 1.如果ubuntu还不能上网的话,可以到我们的电脑的“服务”里面,检查“VMware NA ...
- jq动画效果慢慢滚动到固定位置
function contentTop(top){ $('body,html').animate({ scrollTop: top }, 500 ); } 获取元素top传入即可 contentTop ...
- Linux虚拟机网络连接的三种方式
Bridge桥接模式.NAT模式.Host-only仅主机模式: 桥接模式:虚拟机使用真实网卡进行通信,配置简单:只要和真实计算机在同一个网段内,就可以直接通信:局域网内如果有其他计算机,也可以进行访 ...