每次找到当前最大数,转两下把最大数转到最右边.重复这个操作,直到都转完.
时间复杂度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 retView Code