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. oslo_service服务

    import time from oslo_service import service from oslo_config import cfg class MyService(service.Ser ...

  2. CSS float清除浮动

    解决高度塌陷的问题 – 清除浮动 CSS中有个讨论较多的话题就是如何清除浮动,清除浮动其实就一个目的,就是解决高度塌陷的问题.为什么会高度塌陷?什么时候会高度塌陷?塌陷原因是:元素含有浮动属性 – 破 ...

  3. vue生命周期简介

    vue生命周期简介 生命周期的钩子 LifeCycle hooks 上面已经能够清晰的看到vue2.0都包含了哪些生命周期的钩子函数~~ 那么 执行顺序以及什么时候执行,我们上代码来看~~~ 生命周期 ...

  4. 关于set_output_delay与set_input_delay概念与用法

    一.  这两条约束语句都是针对板级延时而言的. 语句中必须的有是,时钟与port. 二.   set_input_delay 用于数据输入端口,调节数据输入与时钟输入到来的相位关系. 当FPGA外部送 ...

  5. leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)

    传送门:点我 978. Longest Turbulent Subarray A subarray A[i], A[i+1], ..., A[j] of A is said to be turbule ...

  6. e-olymp Problem9 N-digit numbers(打表)

    传送门:点我 N-digit numbers Find the quantity of N-digit numbers, which the sum is equal to their product ...

  7. c# 关闭和重启.exe程序

    Process[] myprocess = Process.GetProcessesByName("a"); if (myprocess.Count() > 0)//判断如果 ...

  8. http://www.bugku.com:Bugku——SQL注入1(http://103.238.227.13:10087/)

    Bugku——SQL注入1(http://103.238.227.13:10087/) 过滤了几乎所有的关键字,尝试绕过无果之后发现,下面有个xss过滤代码.经搜索得该函数会去掉所有的html标签,所 ...

  9. swift - 加速器/摇一摇功能

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  10. Js或 Activex 控件调用打印预览等操作

    <input value="打印" type="button" onclick="javascript:window.print()" ...