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下标位置。

posted on 2019-12-29 13:56  Sempron2800+  阅读(148)  评论(0编辑  收藏  举报