896.Montonic Array - LeetCode
Question


Solution
题目大意:
类似于数学中的减函数,增函数和物理中的加速度为正或为负
思路:
先比较前两个是大于0还是小于0,如果等于0就比较第2,3两个,依次类推,得到这个是递增数组还递减数组后再遍历接下来的数就好办了
Java实现:
public boolean isMonotonic(int[] A) {
if (A.length == 1) return true;
int compFlag = 0;
int i = 1;
while (compFlag == 0 && i < A.length) {
compFlag = A[i] - A[i - 1];
i++;
}
while (compFlag > 0 && i < A.length) {
if (A[i] - A[i - 1] < 0) return false;
i++;
}
while (compFlag < 0 && i < A.length) {
if (A[i] - A[i - 1] > 0) return false;
i++;
}
return true;
}
896.Montonic Array - LeetCode的更多相关文章
- 896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- [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
原题链接在这里:https://leetcode.com/problems/monotonic-array/ 题目: An array is monotonic if it is either mon ...
- 【LeetCode】896. Monotonic Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
随机推荐
- leetcode_两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同 ...
- (stm32学习总结)—spi基本原理
参考:spi详解 spi协议 SPI的基本介绍 SPI的简介 SPI,是英语Serial Peripheral interface的缩写,顾名思义就是串行外围设备接口,是Motorola首先在其M ...
- 深入Linux 内核架构之 CFS
linux内核分析--CFS(完全公平调度算法) 1.1 CFS原理 cfs定义了一种新的模型,它给cfs_rq(cfs的run queue)中的每一个进程安排一个虚拟时钟,vruntime.如果 ...
- numpy计算数组中满足条件的个数
Numpy计算数组中满足条件元素个数 需求:有一个非常大的数组比如1亿个数字,求出里面数字小于5000的数字数目 1. 使用numpy的random模块生成1亿个数字 2. 使用Python原生语法实 ...
- 12_非线性理论基础_Lyapunov直接方法
- Java中 i++和++i 的区别
学习目标: 理解i++和++i的区别 学习内容: 1.i++ / i- - i++/i- -:遇到 i++或 i- -,i先参与运算,然后 i 再自加或自减1 代码如下: int a = 1; int ...
- window nginx 简单搭建服务器访问静态资源
nginx命令: 启动: start nginx 停止:nginx -s stop || nginx -s quit 注:stop是快速停止nginx,可能并不保存相关信息:quit是完整有序的停止 ...
- oracle三个重要参数文件:pfile和spfile和init.ora
Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动第一个阶段时候加载的, 决定了数据库的物理 结构.内存.数据库的限制及系统大量的默认值.数据库的各种物理 ...
- random模块、os模块、序列化模块、sy模块s、subprocess模块
random随机数模块 random.random( ) 随机产生一个0-1之间的小数 print(random.random()) # 0.31595547439342897 random.rand ...
- toggleClass() 把本来的有的类名去掉 本来没有的 加上
连续点击按钮可以交替颜色,就是改变class