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 ...
随机推荐
- TOP 10 开源的推荐系统简介
最 近这两年推荐系统特别火,本文搜集整理了一些比较好的开源推荐系统,即有轻量级的适用于做研究的SVDFeature.LibMF.LibFM等,也有重 量级的适用于工业系统的 Mahout.Oryx ...
- Qunee for HTML5 v1.6新版本发布
Qunee for HTML5 V1.6正式发布,修复了一些 BUG,增加了滚动条支持,改进了编辑器,增加了JSON 导入导出.告警冒泡.连线流动,UI 定制等扩展示例,欢迎 访问 导航面板 增加了滚 ...
- 利用angular4和nodejs-express构建一个简单的网站(一)——构建前后端开发环境
学习了一段时间的angular4知识,结合以前自学的nodejs-express后端框架知识,做了一个利用angular4作为前端,node-express作为后端服务器的网站.这个网站的功能很简单, ...
- 面试题:给你个id,去拿到name,多叉树遍历
前天面试遇到一个多叉树面试的题目,在这里分享记录一下. 题目:一个树形的数据(如下数据),面试官给你一个id,然后拿到对应的name? 数据结构大概是这个样子 var cityData = [ { i ...
- Java/C++实现观察者模式--股票价格
当股票的价格上涨或下降5%时,会通知持有该股票的股民,当股民听到价格上涨的消息时会买股票,当价格下降时会大哭一场. 类图: Java代码: public class Investor implemen ...
- Android开发小经验
1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSiz ...
- jdbc连接MySQL数据库+简单实例(普通JDBC方法实现和连接池方式实现)
jdbc连接数据库 总结内容 1. 基本概念 jdbc的概念 2. 数据库连接 数据库的连接 DAO层思想 重构设计 3. 事务 概念 事务的ACID属性 事务的操作 4. 连接池 为什么要使用连接池 ...
- [MySQL]IP处理函数inet_aton()和inet_ntoa()
INET_ATON(expr) 给出一个作为字符串的网络地址的"点地址"(如127.0.0.1)表示,返回一个代表该地址数值的整数.地址可以是4或8比特地址. mysql> ...
- 深入理解nodejs的异步IO与事件模块机制
node为什么要使用异步I/O 异步I/O的技术方案:轮询技术 node的异步I/O nodejs事件环 一.node为什么要使用异步I/O 异步最先诞生于操作系统的底层,在底层系统中,异步通过信号量 ...
- v-if 和 v-show 的区别
1.v-if && v-else && v-else-if <span v-if = "4 > 9">hello</span ...