[LeetCode] 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
Note:
1 <= A.length <= 50000
-100000 <= A[i] <= 100000
这道题让我们判断一个数组是否单调,单调数组就是说这个数组的数字要么是递增的,要么是递减的,不存在一会儿递增一会儿递减的情况,即不会有山峰存在。这里不是严格的递增或递减,是允许有相同的数字的。那么我们直接将相邻的两个数字比较一下即可,使用两个标识符,inc 和 dec,初始化均为 true,我们开始时假设这个数组既是递增的又是递减的,当然这是不可能的,我们会在后面对其进行更新。在遍历数组的时候,只要发现某个数字大于其身后的数字了,那么 inc 就会赋值为 false,同理,只要某个数字小于其身后的数字了,dec 就会被赋值为 false,所以在既有递增又有递减的数组中,inc 和 dec 都会变为 false,而在单调数组中二者之间至少有一个还会保持为 true,参见代码如下:
解法一:
class Solution {
public:
bool isMonotonic(vector<int>& A) {
bool inc = true, dec = true;
for (int i = 1; i < A.size(); ++i) {
inc &= (A[i - 1] <= A[i]);
dec &= (A[i - 1] >= A[i]);
if (!inc && !dec) return false;
}
return true;
}
};
跟上面的解法思路很像,只不过没有用 bool 型的,而是用了整型数字来记录递增和递减的个数,若是单调数组,那么最终在 inc 和 dec 中一定会有一个值是等于数组长度的,参见代码如下:
解法二:
class Solution {
public:
bool isMonotonic(vector<int>& A) {
int inc = 1, dec = 1, n = A.size();
for (int i = 1; i < n; ++i) {
inc += (A[i - 1] <= A[i]);
dec += (A[i - 1] >= A[i]);
}
return (inc == n) || (dec == n);
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/896
参考资料:
https://leetcode.com/problems/monotonic-array/
https://leetcode.com/problems/monotonic-array/discuss/165889/C%2B%2BJavaPython-One-Pass-O(N)
https://leetcode.com/problems/monotonic-array/discuss/172578/Java-O(n)-simple-solution
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 896. Monotonic Array 单调数组的更多相关文章
- 896. Monotonic Array单调数组
[抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array 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
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
- 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 解题报告(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 ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
随机推荐
- Element-ui上传图片按顺序展示
背景 不知道你上传图片的时候有没有过这样的情况,批量上传多张图片,可能因为图片大小或者网络问题,导致图片返回的顺序和上传时的顺序不一样.因为我们公司是做电商的,即使我们的支持拖动排序,运营还是希望图片 ...
- LeetCode 394:字符串解码 Decode String
题目: 给定一个经过编码的字符串,返回它解码后的字符串. Given an encoded string, return its decoded string. 编码规则为: k[encoded_st ...
- Easyui datagrid扩展子网格detailview增删改查详解
话不多gang,先上代码,将以下三个属性插入主网格的初始化参数中: view : detailview, //1 detailFormatter : function(index, row) { // ...
- 一个比 AutoMapper 更快的模型映射的组件 Mapster
下面是官方的性能测试 Demo,感性的也可以去 Github 上下载. 贴出代码目的是如果后期直接从自己的博客中在线看. using System; using System.Collections. ...
- 使用 Logstash 和 JDBC 确保 Elasticsearch 与关系型数据库保持同步
为了充分利用 Elasticsearch 提供的强大搜索功能,很多公司都会在既有关系型数据库的基础上再部署Elasticsearch.在这种情况下,很可能需要确保 Elasticsearch 与所关联 ...
- Ubuntu关机重启后 NVIDIA-SMI 命令不能使用
问题: 电脑安装好Ubuntu系统后,后续安装了显卡驱动.CUDA.cuDNN等软件,后续一直没有关机.中间系统曾经有过升级,这也是问题所在.系统升级导致内核改变,并可能导致它与显卡驱动不再匹配,所以 ...
- 关于如何提高缓存命中率(redis)
一.缓存命中率的介绍 命中:可以直接通过缓存获取到需要的数据. 不命中:无法直接通过缓存获取到想要的数据,需要再次查询数据库或者执行其它的操作.原因可能是由于缓存中根本不存在,或者缓存已经过期. 通常 ...
- C# 随机 抽奖 50个随机码 不重复
static List<int> Given50RandomNumbers() { List<int> intList = new List<int>(); for ...
- IP 跟踪
#coding=utf-8import sysimport os import re import urllibimport subprocess def getlocation(ip): resul ...
- 设计模式之设计原则 C#
成为一名资深架构师首先要懂设计模式,在懂之前,要清楚设计原则,原来我就吃过这个亏,很久以前有人问我设计原则,我是一头茫然,不是只有设计模式吗?且不知设计原则就像是写书法一样,楷体就是方正,竖道有垂露等 ...