leetcode1299

 1 class Solution:
 2     def replaceElements(self, arr: List[int]) -> List[int]:
 3         n = len(arr)
 4         maxright = arr[-1]
 5         res = [-1]
 6         for i in range(n-2,-1,-1):
 7             right = arr[i+1]
 8             maxright = max(right,maxright)
 9             res.insert(0,maxright)
10         return res

从右向左遍历,每次更新右区间的最大值maxright,并将这个值插入结果数组的0下标位置。

原文地址:https://www.cnblogs.com/asenyang/p/12114877.html