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 是单调 ...
随机推荐
- Docker 镜像/容器操作命令
一.镜像操作 1.拉取镜像 # docker pull tomcat # docker pull tomcat:8.0.21-jre8 # docker pull 192.168.220.150: ...
- Unity Shader 屏幕后效果——Bloom外发光
Bloom的原理很简单,主要是提取渲染图像中的亮部区域,并对亮部区域进行模糊处理,再与原始图像混合而成. 一般对亮部进行模糊处理的部分采用高斯模糊,关于高斯模糊,详见之前的另一篇博客: https:/ ...
- 【linux】查看某个进程PID对应的文件句柄数量,查看某个进程当前使用的文件句柄数量
================================ 1.linux所有句柄查询 lsof -n|awk '{print $2}'|sort|uniq -c |sort -nr|more ...
- sqlserver之datepart和datediff应用查找当天上午和下午的数据
DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. DATEDIFF() 函数返回两个日期之间的时间差. --查找当天上午的数据 ) --查找当天下午的数据 ) ...
- 记录RFID操作错误
如果代码操作不了RFID设备,查看下通信协议的设置
- 我是如何一步步编码完成万仓网ERP系统的(四)登录的具体实现
https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...
- 处理 unassigned shard
#查看所有分片 GET _cat/shards curl 10.1.2.2:9200/_cat/indices/iis_log* #查看索引的分片状态 #查看第一个unassigned shard的 ...
- Celery简介以及Django中使用celery
目录 Celery简介 消息中间件 任务执行单元 任务结果存储 使用场景 Celery的安装和配置 Celery执行异步任务 基本使用 延时任务 定时任务 异步处理Django任务 案例: Celer ...
- Winform中实现根据配置文件重新加载ZedGraph属性的实现思路
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- 外汇盈利EA
>>>>>>>>>>>>>>>>>>>>>>>>> ...