问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

如果数组是单调递增或单调递减的,那么它是单调的。

如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= j,A[i]> = A[j],那么数组 A 是单调递减的。

当给定的数组 A 是单调数组时返回 true,否则返回 false。

输入:[1,2,2,3]

输出:true

输入:[6,5,4,4]

输出:true

输入:[1,3,2]

输出:false

输入:[1,2,4,5]

输出:true

输入:[1,1,1]

输出:true

提示:

1 <= A.length <= 50000

-100000 <= A[i] <= 100000


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.

Input: [1,2,2,3]

Output: true

Input: [6,5,4,4]

Output: true

Input: [1,3,2]

Output: false

Input: [1,2,4,5]

Output: true

Input: [1,1,1]

Output: true

Note:

1 <= A.length <= 50000

-100000 <= A[i] <= 100000


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = { 1, 2, 5 }; var res = IsMonotonic(nums);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsMonotonic(int[] A) {
return isMonotonicIncrease(A) || isMonotonicReduce(A);
} private static bool isMonotonicIncrease(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] < A[i]) {
return false;
}
}
return true;
} private static bool isMonotonicReduce(int[] A) {
for(var i = 0; i < A.Length - 1; i++) {
if(A[i + 1] > A[i]) {
return false;
}
}
return true;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3760 访问。

True

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. 高性能JavaScript--加载和执行(简要学习笔记一)
  2. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
  3. [翻译]用 Puppet 搭建易管理的服务器基础架构(1)
  4. WPF 绑定枚举值
  5. KMP算法学习
  6. iOS开发三步搞定百度推送
  7. Mac OS X 安装ruby环境
  8. c# 靠谱的bitmap转byte[]
  9. linux下的三种解压文件的命令?
  10. (笔记)angular 单选选项卡
  11. Wi-Fi定位,AP定位
  12. android http协议post请求方式
  13. 【Linux】CentOS 学习笔记之二(命令)
  14. dubbo服务简单搭建
  15. branchynet
  16. Codeforces 305E Playing with String 博弈
  17. Redis-Sentinel Redis的哨兵模式
  18. Exception 02 : java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
  19. Nmap命令的常用实例
  20. FFmpeg Basics学习笔记(1)ffmpeg基础

热门文章

  1. Python Ethical Hacking - VULNERABILITY SCANNER(1)
  2. cropper.js 二次开发:截图并下载图片
  3. MySQL数据库---库的操作
  4. 记录一次JSON数据处理(省市区数据)
  5. 将数组内的元素循环左移P个位置
  6. [leetcode/lintcode 题解] 谷歌面试题:找出有向图中的弱连通分量
  7. netcore RabbitMQ入门--win10开发环境
  8. 跟老刘学运维day02~新手必须掌握的Linux命令(2)
  9. layui常用插件(二) 时间插件
  10. Numpy修改数组中的元素值