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

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

相关文章:

  • 2022-12-23
  • 2021-10-29
  • 2021-12-06
  • 2021-11-20
  • 2021-07-22
  • 2021-06-27
  • 2022-01-27
  • 2022-03-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2021-07-07
  • 2022-12-23
  • 2021-04-08
  • 2022-12-23
相关资源
相似解决方案