LeetCode 896. Monotonic Array
原题链接在这里:https://leetcode.com/problems/monotonic-array/
题目:
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
Return true if and only if the given array A is monotonic.
Example 1:
Input: [1,2,2,3]
Output: true
Example 2:
Input: [6,5,4,4]
Output: true
Example 3:
Input: [1,3,2]
Output: false
Example 4:
Input: [1,2,4,5]
Output: true
Example 5:
Input: [1,1,1]
Output: true
Note:
1 <= A.length <= 50000-100000 <= A[i] <= 100000
题解:
Having a direction as d. If it is increasing, d = 1. If it is decreasing, d = -1.
When seeing a increasing, but d = -1, that means there is decreasing before, return false.
Vice Versa.
Time Complexity: O(n). n = A.length.
Space: O(1).
AC Java:
class Solution {
public boolean isMonotonic(int[] A) {
if(A == null || A.length == 0){
return true;
}
int d = 0;
for(int i = 1; i<A.length; i++){
if(A[i] > A[i-1]){
if(d < 0){
return false;
}
d = 1;
}else if(A[i] < A[i-1]){
if(d > 0){
return false;
}
d = -1;
}
}
return true;
}
}
LeetCode 896. Monotonic Array的更多相关文章
- [LeetCode] 896. Monotonic Array 单调数组
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- 896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- 896. Monotonic Array单调数组
[抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...
- 896. Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode 896. 单调数列(Monotonic Array)
896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...
随机推荐
- netcore与ef资料收集
http://www.cnblogs.com/cgzl/p/7661805.html https://www.cnblogs.com/cgzl/p/7675485.html https://www.c ...
- (十)golang--运算符
1.算术运算符 + - * / % ++ -- 使用细节:(1)对于"/"号,整数除和小数除的区别: (2)++和--只能独立使用,即a=a++是不允许的:没有++a和--a:i ...
- 脱离 WebView 的通信 JavaScriptCore
JavascriptCore JavascriptCore 一直作为 WebKit 中内置的 JS 引擎使用,在 iOS7 之后,Apple 对原有的 C/C++ 代码进行了 OC 封装,成为系统级的 ...
- JAVA学习之开发环境配置
JAVA SDK 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 版 ...
- iTextSharp生成pdf含模板(一)---制作pdf模板
参考地址:https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 一.使用场景:在线填写一些信息,根据对应的信息生成奖状. 二.解决方案 1.新建w ...
- vue同一个路由,但参数发生变化,页面不刷新的问题(vue监听路由参数变化重新渲染页面)
watch: { $route: function(newVal, oldVal) { console.log(oldVal); //oldVa 上一次url console.log(newVal); ...
- laravel在使用Composer安装插件时要求输入授权用户名密码解决办法
在使用laravel-china源时需要输入密码,坑,换源, 先换腾讯的不行,最后试一下阿里云的可以: composer config -g repo.packagist composer https ...
- Mac 下安装 jdk
1.安装jdk 我们是需要java环境的- 到oracle官网下载se: Java SE Development Kit 8 Downloads https://www.oracle.com/tech ...
- HTML中的音频 视频 的播放代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- django logger转载
https://www.cnblogs.com/jiangchunsheng/p/8986452.html https://www.cnblogs.com/jeavy/p/10926197.html ...