原题链接在这里: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 <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[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. 1 <= A.length <= 50000
  2. -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的更多相关文章

  1. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  2. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

  3. 896. Monotonic Array@python

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  4. 【Leetcode_easy】896. Monotonic Array

    problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...

  5. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode&Python] Problem 896. Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  7. 896. Monotonic Array单调数组

    [抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...

  8. 896. Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  9. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

随机推荐

  1. hdu6546 Function

    Function \(\text{Alice}\) 有 \(n\) 个二次函数 \(F_i(x)=a_ix^2+b_ix+c_i(i \in [1,n])\). 现在他想在 \(\sum_{i=1}^ ...

  2. 【maven】pom.xml的exclusions排除依赖传递

    在引用两个有冲突的依赖时,就需要把其中一个的依赖中某个依赖排除掉 exclusions 例如: <dependency> <groupId>org.activiti</g ...

  3. OpenShift下的JVM监控

    去年写过一篇基于jmx监控的文章,这次在Openshift上实现,发现确实不少变化.主要重点问题在 1. prometheus jmx exporter的改进,不再需要运行一个独立的进程,不需要把数据 ...

  4. 安装Office 2016 出现 Office 16 Click-to-Run Extensibility Component

    无法安装 64 位版本的 Office,因为在您的 PC 上找到了以下 32 位程序: Office 16 Click-to-Run Extensibility Component 请卸载所有 32 ...

  5. CentOS7 安装nginx-1.14.0

    nginx源码包:http://nginx.org/en/download.html 1.安装gcc gcc是用来编译下载下来的nginx源码 yum install gcc-c++ 2.安装pcre ...

  6. Docker 部署ELK之Sentinl日志报警

    前篇文章简单介绍了Docker 部署ELK,以及使用filebeat收集java日志.这篇我们介绍下日志报警配置,这里我们使用Sentinl插件. 1.修改kibana参数 进入elk容器,修改对应参 ...

  7. ['1', '2', '3'].map(parseInt) 输出答案和解析

    根据题目可以了解到这道题主要考我们的是对map函数和parseInt函数的熟悉程序,所以我们先来了解这两个函数 map 根据MDN上对于map的解释:map() 方法创建一个新数组,其结果是该数组中的 ...

  8. ImageView的adjustViewBounds属性

    adjustViewBounds属性的定义如下: 调整ImageView的边界,使得ImageView和图片有一样的宽高比 这个属性只有在ImageView一边如宽度或高度固定,一边为wrap_con ...

  9. Kafka学习笔记2--Kafka的服务端配置

    下载解压 kafka 后,在 kafka/config 下有 3 个配置文件与主题的生产.消费相关. server.properties--服务端配置 producer.properties--生产端 ...

  10. C# 与 Java 的一些差异

    如果你是 Java 开发人员,则可以在 Xamarin 平台上充分利用你的技能和现有代码,同时获得 C# 的代码重用优势.你会发现 C# 语法与 Java 语法非常相似,这两种语言提供非常类似的功能. ...