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 ...
随机推荐
- Spring注解标签详解@Autowired @Qualifier等
http://blog.csdn.net/wangsr4java/article/details/42777855 @Component.@Repository.@Service.@Controlle ...
- datetime is not json serializable
python, datetime is not json serializable import datetime def json_serial(obj): """JS ...
- 自己写一个spring boot starter
https://blog.csdn.net/liuchuanhong1/article/details/55057135
- js Json数组的增删改查
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- 转: 日期格式参考extjs api文档中的Date类型
var md = new Ext.form.DateField({ //下面的格式是:2000-01-01 00:00:00 format: 'Y-m-d H:i:s', ............ } ...
- [cocos2d-x]认识游戏开发(图)
FreeMind的.mm文件下载: http://yunpan.cn/cfL3cm6CZkMSt (提取码:e01a)
- img标签插入图片返回403,浏览器可以直接打开
参考:https://segmentfault.com/q/1010000011752614/a-1020000011764026 博客园引入外部图片出现,出现403问题,应该是加了防盗链,会检测访问 ...
- 使用jQuery+huandlebars遍历展示对象中的数组
兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...
- java Overloaded的方法是否可以改变返回值的类型?
刚才看到这样一个题,下面的解释很乱,所以还是做一下试验比较好 public class Test { public static void main(String[] args){ Bae b = n ...
- 源码编译安装Python3及问题解决
https://chowyi.com/%E6%BA%90%E7%A0%81%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85Python3%E5%8F%8A%E9%97%AE%E ...