题目描述:
自己的提交:
class Solution: def transformArray(self, arr: List[int]) -> List[int]: if len(arr) < 3: return arr flag = True while flag: tmp = [] tmp.append(arr[0]) for i in range(1,len(arr)-1): if arr[i] > arr[i-1] and arr[i] > arr[i+1]: tmp.append(arr[i] - 1) elif arr[i] < arr[i-1] and arr[i] < arr[i+1]: tmp.append(arr[i] + 1) else: tmp.append(arr[i]) tmp.append(arr[-1]) if arr == tmp: flag = False arr = tmp return arr
优化:
class Solution: def transformArray(self, A: List[int]) -> List[int]: change = 1 n = len(A) while change: change = 0 B = A[:] for i in range(1, n - 1): if A[i-1] < A[i] > A[i+1]: B[i] -= 1 change = 1 if A[i-1] > A[i] < A[i+1]: B[i] += 1 change = 1 A = B return B
原文地址:https://www.cnblogs.com/oldby/p/11797701.html
时间: 2024-11-01 12:15:00