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 是单调 ...
随机推荐
- c# mongodb时间类型字段保存时相差八个小时解决办法
/// <summary> /// 添加时间 /// </summary> [BsonDateTimeOptions(Kind = DateTimeKind.Local)] p ...
- C# 使用ConcurrentBag类处理集合线程安全问题
在日常的开发中,经常会遇到多个线程对同一个集合进行读写操作,就难免会出现线程安全问题. 以下代码,如果使用List<T>就会遇到问题:System.InvalidOperationExce ...
- 【RS】A review on deep learning for recommender systems: challenges and remedies- 推荐系统深度学习研究综述:挑战和补救措施
[论文标题]A review on deep learning for recommender systems: challenges and remedies (Artificial Intell ...
- Vue.js+vue-element
Vue.js+vue-element搭建属于自己的后台管理模板:什么是Vue.js?(一) Vue.js+vue-element搭建属于自己的后台管理模板:Vue.js是什么?(一) 前言 本教程 ...
- PhpStorm注册使用方法
解压 sudo tar -zvxf PhpStorm-2019.3.tar.gz -C /usr/local 屏蔽hosts # Phpstorm 0.0.0.0 account.jetbrains. ...
- ReentrantReadWriteLock三个线程读数据,三个线程写数据
/*** * 三个线程读数据,三个线程写数据 * */ public class ReadWriteLockTest { public static void main(String[] args) ...
- SQL忽略重复键作用
1.插入时如果开启的话,发现重复键会忽略,否则报错 2.更新时不管开启是否都会报错
- springboot: xercesImpl.jar和xml-apis.jar (系统找不到指定的文件)
springboot内置的tomcat为8.5.23, tomcat在8.5.2 中 修改了加载jar的方式,8.5.2 版本会解析jar中MANIFEST.MF文件,当该文件包含class-path ...
- ZYNQ笔记(2):PS端——Hello World !
PL端使用过后,来到了ZYNQ核心的部分:PS端,现在用Vivado软件对ZYNQ-7000开发板的PS端进行第一个程序设计:Hello World. 一.新建Vivado工程 1.打开Vivado, ...
- python网络爬虫(1)——安装scrapy框架的常见问题及其解决方法
Scrapy是为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板. 其实在Linux和 Mac安装,就简单的pip命令即可: pip i ...