An array is monotonic if it is monotonically increasing or monotonically decreasing .
Example 1:
Input: nums = [1,2,2,3]
Output: true
Example 2:
Input: nums = [6,5,4,4]
Output: true
Example 3:
input: nums = [1,3,2]
output: false
Source: LeetCode
Link: https://leetcode.cn/problems/monotonic-array
class Solution(object):
def isMonotonic(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
b=[]
j=1
for i in range(len(nums)):
if j>=len(nums):
break
if nums[i]==nums[j]:
j+=1
continue
c=nums[i]-nums[j]
j+=1
b.append(c)
if all(i>0 for i in b) or all(i<0 for i in b) :
return True
else:
return False