博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 969. Pancake Sorting
阅读量:4656 次
发布时间:2019-06-09

本文共 588 字,大约阅读时间需要 1 分钟。

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

时间复杂度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
View Code

 

转载于:https://www.cnblogs.com/zywscq/p/10540567.html

你可能感兴趣的文章