896. 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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
想到了用inc dec变量来记录,以为要判断第一个:不用,两边同时加就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
自己跑Case很重要
[复杂度]:Time complexity: O(N) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean isMonotonic(int[] A) {
//corner case
if (A == null || A.length == 0)
return true;
//for loop
int inc = 0; int dec = 0;
/*
1 2 2 3
inc 1 2 3
dec 1
*/
for (int i = 1; i < A.length; i++) {
if (A[i] > A[i -1]) inc++;
else if (A[i] < A[i -1]) dec++;
else {
inc++;
dec++;
}
}
//return
return (inc == A.length - 1) || (dec == A.length - 1);
}
}
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 ...
- 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>& ...
- Leetcode896.Monotonic Array单调数列
如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> = ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- 【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 is mono ...
- LeetCode 896. Monotonic Array
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
随机推荐
- Django 之Form
具体可参考:http://www.liujiangblog.com/course/django/153 https://www.cnblogs.com/liuguniang/p/7141837.htm ...
- ReactiveX 学习笔记(15)使用 Rx.NET + Json.NET 调用 REST API
JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...
- .Net编译原理简单介绍
首先简单说一下计算机软件运行.所谓软件运行,就是一步一步做一些事情.计算机只认识0和1.给计算机下命令,只能是0与1的方式,确切的说,其实是CPU只认识0和1,因为软件运行是CPU控制的.人直接操作0 ...
- avalon1与avalon2的异同点
avalon2并不向下兼容avalon1,但许多API与指令很相似,迁移成本比较低.此外,良好的性能与强大的功能是你迁移的动力.下面是一个列表,如有提问尽管提出. avalon1与avalon2的异同 ...
- FMS Dev Guide学习笔记(SharedBall)
一.开发交互式的媒体应用程序1.共享对象(Shared objects) ----SharedBall example 这个SharedBall example创建了一个临时的远程共享对象.类似于多人 ...
- springboot 集成dubbo
- C# 反射赋值
tb_Projects model = new tb_Projects(); model.OwnerId = ; string FieldName = "OwnerId";//字段 ...
- ADO.Net 综合练习题
题目: 第一部分: 新建一个数据库:ADO测试,包含下面两个数据表,使用代码创建,并保留创建的代码文本. 专业表Subject: 专业编号(SubjectCode):nvarchar类型,不能为空,主 ...
- idea 设置 转自 https://www.cnblogs.com/jajian/p/8136672.html
前面已经介绍过Settings上中部分,接下来继续剩余的部分 IntelliJ IDEA(四) :Settings(上) IntelliJ IDEA(五) :Settings(中) 0|1一.Buil ...
- webstocket 聊天
/** * 初始化socket **/ function initSocket(index_host){//端口号 if( !window.WebSocket ){ console.log(" ...