LeetCode 896. 单调数列(Monotonic Array)
896. 单调数列
896. Monotonic Array
题目描述
如果数组是单调递增或单调递减的,那么它是单调的。
如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调递增的。如果对于所有 i<=j,A[i]>=A[j],那么数组 A 是单调递减的。
当给定的数组 A 是单调数组时返回 true,否则返回 false。
LeetCode896. Monotonic Array
示例 1:
输出:true
示例 2:
输出:true
示例 3:
输出:false
示例 4:
输出:true
示例 5:
输出:true
Java 实现
class Solution {
// 个人思路
public boolean isMonotonic1(int[] A) {
int n = A.length;
int count1 = 1, count2 = 1;
for (int i = 0; i < A.length - 1; i++) {
if (A[i] <= A[i + 1]) {
count1++;
}
if (A[i] >= A[i + 1]) {
count2++;
}
}
return count1 == n || count2 == n;
}
// 参考思路
public boolean isMonotonic(int[] A) {
boolean inc = true, dec = true;
for (int i = 1; i < A.length; ++i) {
inc &= A[i - 1] <= A[i];
dec &= A[i - 1] >= A[i];
}
return inc || dec;
}
}
参考资料
LeetCode 896. 单调数列(Monotonic Array)的更多相关文章
- [Swift]LeetCode896. 单调数列 | Monotonic Array
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- LeetCode.896-单调数组(Monotonic Array)
这是悦乐书的第345次更新,第369篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第210题(顺位题号是896).如果数组单调递增或单调递减,则数组是单调的.如果对于所有 ...
- 力扣896. 单调数列-C语言实现-简单题
题目 传送门 文本 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j, ...
- 领扣(LeetCode)单调数列 个人题解
如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> = ...
- 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:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
随机推荐
- Java 冒泡排序算法
public class StringSampleDemo { public static void main(String[] args) { int[] arr = {1, 2, -20, 20, ...
- python中普通函数调用协程
import asyncio def target(loop, timeout=None): future = asyncio.run_coroutine_threadsafe(add(1, b=2) ...
- Mysql 查看所有线程,被锁的表等
## 查看所有MYSQl相关的线程 > show full processlist; ## 杀死线程id为2的线程 > kill 2 ## 查看服务器状态 > show status ...
- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'kindergarten.n.stuMChkTime' which is not functionally dependent on columns in GROUP BY clause; this is in
错误原因: sql _mode中only _full _group _by不兼容的问题 解决思路: 既然是only _full _group _by不兼容,那就把它去掉就好啦 show var ...
- 关于PHP中token的生成的解析
背景 很多时候我们需要用 token 来作为一些标识, 比如: 一个用户登录后的认证标识. 实现方式 md5 的方式: $v = 1; // 自己定义的 需要hash 的value 值 $key = ...
- Spring’s RestTemplate
Spring’s RestTemplate /** * After the word document is generated in memory we can upload it to the s ...
- android strings: %s、%1$s、%d、%1$d占位符
实际开发的过程中我们有时候会遇到,一个TextView里面会遇到会有一个一大串固定的文字,而里面的数字或者个别字需要根据后台的接口而展示的.这个时候我们最简单的方法就是在string.xml文件里 使 ...
- Android开发Camera2相关
Android自定义相机 https://github.com/miqt/camera2 Camera2必知必会 https://www.cnblogs.com/fuyaozhishang/p/975 ...
- Java基础 main 参数String[] args的用法
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- leetcode 384. Shuffle an Array
384. Shuffle an Array c++ random函数:https://www.jb51.net/article/124108.htm rand()不需要参数,它会返回一个从0到最大随机 ...