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 ...
随机推荐
- IP地址的含义
不管是学习网络还是上网,IP地址都是出现频率非常高的词.Windows系统中设置IP地址的界面如图1所示,图中出现了IP地址.子网掩码.默认网关和DNS服务器这几个需要设置的地方,只有正确设置,网络才 ...
- 一次docker中的nginx进程响应慢问题定位记录
有个ft测试的环境,其中nginx使用docker发布的.测试用例是curl的时候,没有获得nginx的响应. docker ps CONTAINER ID IMAGE COMMAND CREATED ...
- 使用xshell xftp连接centos的过程。
1 配置网络 登录Centtos系统,配置网卡. cd 空格/etc/sysconfig/network-scripts 回车键. ls 查看文件 vi ifcig-eth0 编辑这个文件 修改为 ...
- (转)MySQL 获得当前日期时间 函数
select *from High_valwhere SerialDate >= curdate() and SerialDate < date_add(curdate(), interv ...
- 将一个dropdownlist从一个div复制到另一个div
<select id="dropdwon1"> <option value=">Item1</option> <option v ...
- C# 递归获取 文件夹的 所有文件
public void Director(string dir, List<string> list) { DirectoryInfo d = new DirectoryInfo(dir) ...
- Java中String的常用方法总结
Java中String的常用方法总结 1.length()字符串的长度 String str="HelloWord"; System.out.println(str.length( ...
- SXSSExcelUtil
package com.numa.util; import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.util ...
- python基础之字符串常用操作总结
字符串的索引 s = 'ABCDLSESRF' # 索引 这两个很简单没什么说的 s1 = s[0] print(s1) # A s2 = s[2] print(s2) # C 切片 s = 'ABC ...
- V4 V7 V13支持包的区别(转)
三者均为支持包,可以让低版本系统使用高版本特性,支持最小版本有差异 V4支持1.6以上 V7支持2.1以上 V13支持3.2以上 V7依赖V4 转自: