题目标签:Array

题目给了一组int array A,让我们判断它是否是 一个山脉数组。

山脉数组一定要有一个最高值,然后要同时有 山坡和下坡。

想法是,从左边开始依次比较两个数字,int[0] int[1].... 如果是上坡,继续寻找,如果遇到了下坡,停止,这样就找到了最高点。

从右边也是同样的操作。

然后排除只有上坡的,或者只有下坡的情况。

最后比较一下, 左边找到的最高点和右边找到的最高点是否是同一个点。

具体看code。

Java Solution:

Runtime beats 99.95%

完成日期:03/05/2019

关键点:从左右两个方向各自找最高点

class Solution
{
public boolean validMountainArray(int[] A)
{
if(A.length < 3)
return false; int left = 0;
int right = A.length - 1; // from left: climb the hill until the highest reached
while(left + 1 < A.length && A[left] < A[left + 1])
left++;
// from right: climb the hill until the highest reached
while(right - 1 > 0 && A[right] < A[right - 1])
right--; if(left == A.length - 1) // only uphill
return false;
else if(right == 0) // only downhill
return false; return left == right;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 941. Valid Mountain Array (有效的山脉数组)的更多相关文章

  1. 【Leetcode_easy】941. Valid Mountain Array

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

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

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

  3. 【leetcode】941. Valid Mountain Array

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

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

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

  5. [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 ...

  6. Valid Mountain Array LT941

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

  7. 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 ...

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

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

  9. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

随机推荐

  1. DeepMind:所谓SACX学习范式

    机器人是否能应用于服务最终还是那两条腿值多少钱,而与人交互,能真正地做"服务"工作,还是看那两条胳膊怎么工作.大脑的智能化还是非常遥远的,还是先把感受器和效应器做好才是王道. 关于 ...

  2. HDU_1072_Nightmare

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需 ...

  3. python利用requests统计1个接口的响应时间

    参照 https://www.cnblogs.com/yoyoketang/p/8035428.html requests统计接口的响应时间有2种方式 r.elapsed.total_seconds( ...

  4. yii 在lnmp下访问问题

    lnmp大坑 /usr/local/nginx/conf/fastcgi.conf  文件里面

  5. [Algorithm] 11. Linked List Cycle

    Description Given a linked list, determine if it has a cycle in it. To represent a cycle in the give ...

  6. Python学习笔记之生成器、迭代器和装饰器

    这篇文章主要介绍 Python 中几个常用的高级特性,用好这几个特性可以让自己的代码更加 Pythonnic 哦 1.生成器 什么是生成器呢?简单来说,在 Python 中一边循环一边计算的机制称为 ...

  7. 谈谈TCP中的TIME_WAIT

    所以,本文也来凑个热闹,来谈谈TIME_WAIT. 为什么要有TIME_WAIT? TIME_WAIT是TCP主动关闭连接一方的一个状态,TCP断开连接的时序图如下: 当主动断开连接的一方(Initi ...

  8. Grid Convergence Index-- Post Processing in CFD

    t Grid Convergence Index Table of Contents 1. Grid/mesh independence   GCI 1.1. Richardson extrapola ...

  9. convert images to a video (Ubuntu)

    use =avconv= package e.g.  to convert images (v_1.png, v_2.png ...) to 'velocity.mp4' >>> a ...

  10. PAT 1138 Postorder Traversal

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...