Given an array A of integers, return true if and only if it is a valid mountain array.

Recall that A is a mountain array if and only if:

  • A.length >= 3
  • There exists some i with 0 < i < A.length - 1 such that:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[B.length - 1]

Example 1:

Input: [2,1]
Output: false

Example 2:

Input: [3,5,5]
Output: false

Example 3:

Input: [0,3,2,1]
Output: true

Note:

    1. 0 <= A.length <= 10000
    2. 0 <= A[i] <= 10000 

Idea 1.  遍历验证是否up-down once

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public boolean validMountainArray(int[] A) {
int n = A.length;
// if(n < 3) {
// return false;
// } int left = 0;
while(left+1 < n && A[left] < A[left+1]) {
++left;
}
if(left == 0 || left == n-1) {
return false;
} while(left+1 < n && A[left] > A[left+1]) {
++left;
} if(left == n-1) {
return true;
} return false;
}
}

Idea 1. 网上看到的有意思的解法,both two men walking up from the bottom

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public boolean validMountainArray(int[] A) {
int n = A.length; int left = 0;
while(left+1 < n && A[left] < A[left+1]) {
++left;
} int right = n-1;
while(right - 1 >= 0 && A[right-1] > A[right]) {
--right;
} return left == right && left!= 0 && right != n-1;
}
}

Valid Mountain Array LT941的更多相关文章

  1. [Swift]LeetCode941. 有效的山脉数组 | Valid Mountain Array

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  2. Weekly Contest 111-------->941. Valid Mountain Array(max_element)

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  3. 【Leetcode_easy】941. Valid Mountain Array

    problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...

  4. LeetCode 941. 有效的山脉数组(Valid Mountain Array)

    941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...

  5. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  6. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode 941. Valid Mountain Array (有效的山脉数组)

    题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...

  8. LeetCode.941-有效山形数组(Valid Mountain Array)

    这是悦乐书的第360次更新,第387篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第222题(顺位题号是941).给定一个整数数组A,当且仅当它是一个有效的山形数组时返回 ...

  9. LeetCode 852. Peak Index in a Mountain Array C++ 解题报告

    852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...

随机推荐

  1. zabbix 利用python脚本实现短信告警

    一.编写脚本 cd /usr/local/zabbix-4.0.3/share/zabbix/alertscripts vi zabbix_sms.py 内容如下: #!/usr/bin/python ...

  2. centos6安装nginx

    1.获取官方的仓库地址 我们需要先访问nginx的官方网站,获取官方的仓库地址https://nginx.org/en/linux_packages.html#stable 新建/etc/yum.re ...

  3. 使用synchronized wait() notifyall() 实现简单的加减法同步 竞争抢答

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.co ...

  4. Python图片识别找坐标(appium通过识别图片点击坐标)

    ***如果只想了解图片相似度识别,直接看第一步即可 ***如果想了解appium根据图片识别点击坐标,需要看第一.二.三步   背景|在做UI测试时,发现iOS自定义的UI控件,appium识别不到. ...

  5. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  6. Codeforces Beta Round #35 (Div. 2)

    Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看 ...

  7. 初识Netty

    我们已经了解了Socket通信/IO/NIO/AIO编程,对于通信模型已经有了一个初步的认识,其实我们之前所学习的仅仅是一个模型,如果想把这些真正的用于实际工作中去,其实我们之前所学习的仅仅是一个模型 ...

  8. @Transational)的方法,注解失效的原因和解决方法

    在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解是不会生效的. 比如,下面代码例子中,有两方法,一个有@Transational注解,一个没有.如果 ...

  9. WebApi2跨域问题及解决办法

    跨域问题产生的原因 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能.现在所有支持JavaScript的浏览器都会使用这个策略.所谓同源是指,域名,协议, ...

  10. apache开启gzip压缩

    1.在httpd.conf中去掉下面的#号 LoadModule headers_module modules/mod_headers.so LoadModule deflate_module mod ...