1. class Solution {
  2. public:
  3. bool isMonotonic(vector<int>& A) {
  4. if (A.size() <= )
  5. {
  6. return true;
  7. }
  8. bool GetDiff = false;//是都找到了不同元素
  9. bool Increace = true;//假设为增序列
  10. for (int i = ; i <= A.size() - ; i++)
  11. {
  12. if (GetDiff)//已经找到不相同元素
  13. {
  14. if (Increace&&A[i] > A[i + ])//应该是增序列,但遇到左>右,则表示false
  15. {
  16. return false;
  17. }
  18. if (!Increace&&A[i] < A[i + ])//应该是减序列,但遇到左<右,则表示false
  19. {
  20. return false;
  21. }
  22. }
  23. else
  24. {
  25. if (A[i] != A[i + ])//第一次发现不相同元素
  26. {
  27. if (A[i] < A[i + ])// 如果左<右,则后面应该都是增
  28. {
  29. Increace = true;//增序列
  30. }
  31. else//如果左>右,则后面应该都是减
  32. {
  33. Increace = false;//减序列
  34. }
  35. GetDiff = true;
  36. }
  37. }
  38. }
  39.  
  40. return true;//没有出现问题,则表示true
  41. }
  42. };

leetcode896的更多相关文章

  1. [Swift]LeetCode896. 单调数列 | Monotonic Array

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  2. Leetcode896.Monotonic Array单调数列

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

  3. LeetCode 896. 单调数列(Monotonic Array)

    896. 单调数列 896. Monotonic Array 题目描述 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i<=j,A[i]<=A[j],那么数组 A 是单调 ...

随机推荐

  1. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. About GCC

    GCC used to stand for the GNU C Compiler, but since the compiler supports several other languages as ...

  3. linux TCP Fast Open开启和测试

    linux上要开启TCP Fast Open,内核版本至少为3.7.0, 且需要设置 /proc/sys/net/ipv4/tcp_fastopen 为3. 开启后,如果有连接进来,使用如下命令查看: ...

  4. 解决pip安装太慢的问题

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  5. php项目,cpu暴增问题查找

    背景: 前几天通过WordPress上线一个应用(前后台部署分离,后台走内网内部使用,前台做了全站缓存对外使用). 今天访问后台应用发现开始报504,一段时间后全部504. 解决方案: 登录容器发现容 ...

  6. laravel 中将DB::select 得到的内容转为数组

    $sql = "select count(*) as num from api_log where uid='{$this->uid}'";                $ ...

  7. Supervisor进程监控

    安装 yum install -y python-setuptools easy_install supervisor echo_supervisord_conf > /etc/supervis ...

  8. sql_自连接,181,182,196,197

    181. Employees Earning More Than Their Managers https://leetcode.com/problems/employees-earning-more ...

  9. 浅谈如何在SQL Server中生成脚本

    在生成脚本过程中,有很多参数可以选择,合理的配置这些参数,可以让我们很方便的按照我们的期望生成脚本. 生成脚本的一些选项,如下图: 我这里是SQL 2005 的选项, SQL 2008 的选项跟这个稍 ...

  10. JQuery 提示用户名密码不为空

    $(document).ready(function(){                  //HTML()替换   HTML函数         //append()追加  :input = :t ...