LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签: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 (有效的山脉数组)的更多相关文章
- 【Leetcode_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- 【LeetCode】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】941. Valid Mountain Array
题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...
- LeetCode 941. 有效的山脉数组(Valid Mountain Array)
941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode.941-有效山形数组(Valid Mountain Array)
这是悦乐书的第360次更新,第387篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第222题(顺位题号是941).给定一个整数数组A,当且仅当它是一个有效的山形数组时返回 ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- tensorFlow资源
1,[莫烦]Tensorflow tutorials (Eng Sub) 神经网络 http://www.bilibili.com/video/av10118932/index_35.html#pag ...
- CentOS7上安装稻壳CMS
CentOS7上安装稻壳CMS 1, 安装用途 为了给某公司建设一个小型网站,租用了一个阿里云ECS服务器,最基础的硬件配置,因此选择了CentOS7操作系统. 稻壳CMS(docCMS)源于深喉咙C ...
- sql server truncate table 删除表数据限制条件
truncate 注释 注释TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快 ...
- dubbo-monitor安装及配置过程
安装 1. 使用git下载(git clone https://github.com/alibaba/dubbo.git)或者从http://dubbo.io/下载源码 2. cd到dubbo的根目录 ...
- 外观模式(Facade)-子系统的协作与整合-接口模式
对子系统进行整合,对外提供更强大或更便捷的接口. 在一个模块和几个子系统进行通信时考虑. 什么是外观模式? 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口 ...
- 手机端h5复制功能
html: <a href="javascript:;" id="copyBtn" class="f-r tac" data-clip ...
- Android studio升级后原有项目无法正常编译运行问题
Android studio工具升级后Gradle版本问题 背景 升级AndroidStudio到最新版本后,原来可正常编译输出AndroidTest的项目无法正常编译通过. 原因 升级后的Andro ...
- UVA - 1615 Highway(贪心-区间选点问题)
题目: 给定平面上n(n≤105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D. 思路: 先自己造区间,然后贪心选点就可以了.之前做过一 ...
- UVA - 10603 Fill(BFS求最小值问题)
题目: 给出三个杯子(没有刻度线)的容量,起初之后第三个杯子是满的,其他的两个杯子是空的,容量分别是a.b.c.问最少需要倒多少升水才能让某一个杯子中的水有d升?如果不能恰好做到d升,就让某一个杯子里 ...
- Vijos P1334 NASA的食物计划
解法 二维费用01背包问题 解法就是先枚举物品再枚举条件这里两个条件所以要枚举两个for 代码 #include <bits/stdc++.h> using namespace std; ...