原题链接在这里: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. Windwos Server 2016 远程桌面授权

    https://blog.csdn.net/hanzheng260561728/article/details/80443135 第二步激活客户的的时候,注意事项

  2. mybatis使用associaton进行分步查询

    Employee类 public class Employee { private Integer id; private String lastName; private String email; ...

  3. 纯 css 打造一个小提示 tooltip

    最后编辑:2019/11/26 前 无意间在寻找资料时候,发现一个不错的小提示,查看源码竟然是纯手工 css 编写(文章底部参考链接). 效果 使用的特性 css2 中的 attr 函数,所以现在(2 ...

  4. axios源码入口以及公用方法

    axios学习笔记(公用方法) 源码地址 找到入口文件 axios/lib/axios.js var utils = require('./utils'); var bind = require('. ...

  5. yarn安装node-sass失败问题

    注:使用 yarn install 命令安装依赖时报错 第一步:更改镜像源 yarn config set registry https://registry.npm.taobao.org -g 第二 ...

  6. 虚拟机Ubuntu18.04 root下 连接 windows 中 winScp

    先查看自己虚拟机中是否有 ssh服务 如果没有的话先安装 apt-get install openssh-server 安装完之后 先手动开启一下服务 /etc/init.d/ssh restart ...

  7. microsoft.extensions.logging日志组件拓展(保存文本文件)

    Microsoft.Extensions.Logging 日志组件拓展 文件文本日志 文件文本日志UI插件 自定义介质日志 Microsoft.Extensions.Logging.File文件文本日 ...

  8. 史上最全的音视频SDK包分享给大家

    史上最全的音视频SDK包分享给大家 概述一下SDK功能: 项目 详情视频通信  支持多种分辨率的视频通信语音通信  提供语音通信,可支持高清宽带语音动态创建房间  可以根据需要,随时创建房间H5 支持 ...

  9. APS助众生药业突破运营管理瓶颈

    众生药业一直致力于为了世界提供世界级的产品及服务,成立以来公司先后实施了ERP系统,CRM系统,WMS系统,OA系统,精益生产,朝着行业信息化水平领先的目标迈进. 但近年随着业务量的不断扩大,仅仅拥有 ...

  10. Android 解决Execution failed for task ':app:clean.'报错

    说实话,我还真是没见过比Execution failed for task ':app:clean.'更为恶心,更为不要脸的bug啦,代码没啥问题,一下子行让你爽爽,一下子又不鸟你啦,研究了下,终于找 ...