Leetcode 896 .Monotonic Array
==
### Description
(easy)
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].
Given an integer array nums, return true if the given array is monotonic, or false otherwise.
#### 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
### 想法
一開始直接頭尾相比就可以知道這是一格遞增或是遞減的數列,然後再用while把數列過一遍看看有沒有不符合的數,使用的時候記得index<numsize-1 不然我一開始有overflow會比不到最後一個,沒過回傳false有過回傳true
### 程式碼
#### python:
```python=
class Solution:
def isMonotonic(self, nums: List[int]) -> bool:
index=0
if nums[0]>nums[-1]-1:
while index<len(nums)-1:
if nums[index]<nums[index+1]: return False
index+=1
else:
while index<len(nums)-1:
if nums[index]>nums[index+1]: return False
index+=1
return True
```
Runtime: 1036 ms
Memory Usage: 28.1 MB
faster than 92% of python3 users
```c=
bool isMonotonic(int* nums, int numsSize){
int index=0;
if(nums[0]>nums[numsSize-1]){
while(index<numsSize-1){
if(nums[index]<nums[index+1])
return false;
index++;
}
}else{
while(index<numsSize-1){
if(nums[index]>nums[index+1])
return false;
index++;
}
}
return true;
}
```
Runtime: 185 ms
Memory Usage: 14 MB
faster than 75% c users