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 ...
随机推荐
- java中如何获得src路径
代码 解析: 类名.class.get类加载器().getResourceAsStream("文件名"); 案例代码: Demo.class.getClassLoader().ge ...
- 谈谈关于CSS中transform属性之scale
谈谈关于scale属性 scale是什么? 根据W3C定义 ,scale主要是进行缩放和转化: scale能做什么? 1.1px细线 <div class="wrap"> ...
- 实用的 CSS — 贝塞尔曲线(cubic-bezier)
欢迎移步我的博客阅读:<实用的 CSS - 贝塞尔曲线(cubic-bezier)> 前言 在了解 cubic-bezier 之前,你需要对 CSS3 中的动画效果有所认识,它是 anim ...
- 人机交互大作业--flash嵌入web(纯界面)
界面: 源代码:最近较忙,后续会上传至github
- java中什么叫多重捕获MultiCatch,它的用法是怎样的?
2.多重捕获MultiCatch 马克-to-win:什么叫多重捕获MultiCatch?一段代码可能引起多个异常,这时可以定义两个或更多的catch子句来处理这种情况,每个子句捕获一种类型的异常.马 ...
- MySQL---char和varchar的区别
char和varchar的区别 char表示定长, 即长度固定. varchar表示变长, 即长度可变. 当输入数据的长度小于定义的长度时, char会用空格填充, 而varchar则按照实际长度存储 ...
- Shiro之权限管理的概念
文章目录 前言:什么是shiro 一.什么是权限管理? 举例 二.权限管理的具体分类 1.身份认证 2.授权 总结 前言:什么是shiro Apache Shiro 是一个开源安全框架,提供身份验证. ...
- LC-26
class Solution { public int removeDuplicates(int[] nums) { int slowIndex = 0, fastIndex = 1; if (num ...
- Java学习day16
IO流即输入/输出流,按数据类型分为:字节流和字符流 与IO有关的操作最后都要释放,使用close方法 以字节流形式写入数据后需要换行可以添加换行符,注意旧版系统之间识别的换行符不相同,旧版Windo ...
- 2021年3月-第02阶段-前端基础-HTML+CSS阶段-Day03
HTML5 第三天 一. 认识 3D 转换 3D 的特点 近大远小 物体和面遮挡不可见 三维坐标系 x 轴:水平向右 -- 注意:x 轴右边是正值,左边是负值 y 轴:垂直向下 -- 注意:y 轴下面 ...