1 class Solution: 2 def transformArray(self, arr: 'List[int]') -> 'List[int]': 3 n = len(arr) 4 if n < 3: 5 return arr 6 else: 7 arr2 = arr[:] 8 tag = True 9 while tag: 10 i = 1 11 tag = False 12 while i < n-1: 13 if arr[i] > arr[i-1] and arr[i] > arr[i+1]: 14 arr2[i] = arr[i] - 1 15 tag = True 16 elif arr[i] < arr[i-1] and arr[i] < arr[i+1]: 17 arr2[i] = arr[i] + 1 18 tag = True 19 i += 1 20 arr = arr2[:] 21 return arr
这题目描述的不是很清楚,没有说明是每次更新“即时生效”还是每次更新“批量生效”,做过题目的应该能明白我我说的意思。