LeetCode #896. Monotonic Array
作者:互联网
题目
解题方法
先判断单调性,再根据单调性判定数组中剩余数是否满足单调性,返回对应判定结果。
时间复杂度:O(n)
空间复杂度:O(1)
代码
class Solution:
def isMonotonic(self, A: List[int]) -> bool:
i = 1
monoflag = 0
while i < len(A):
if A[i] > A[i-1]:
monoflag = 1
break
elif A[i] < A[i-1]:
monoflag = 0
break
else:
i += 1
i += 1
if monoflag:
while i < len(A):
if A[i] < A[i-1]:
return False
i += 1
else:
while i < len(A):
if A[i] > A[i-1]:
return False
i += 1
return True
标签:return,896,复杂度,monoflag,len,while,Array,LeetCode,单调 来源: https://www.cnblogs.com/RatsCommander/p/14010935.html