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 ...
随机推荐
- JS高级——弹出框的美化
替换原有的alert方法,window.alert=function(){} https://blog.csdn.net/kirsten_z/article/details/76242286 http ...
- Codeforces_779_D.String Game_(二分)
D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- linux 系统 UDP 丢包问题分析思路
转自:http://cizixs.com/2018/01/13/linux-udp-packet-drop-debug?hmsr=toutiao.io&utm_medium=toutiao.i ...
- Xcode5编译ffmpeg
命令行安装FFmpeg:git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg(或:到https://github.com/gabriel/ffmpeg ...
- 操作Session存储对象时的一些问题
前段时间在操作Session存储的对象时,遇到了类似以下的这种情况: User user=new User("张三"); //创建一个姓名为“张三”的User对象 Session[ ...
- 关于DEV-c++ 运行窗口闪退的解决办法
因为程序默认运行结束自动关闭,所以运行窗口会被秒关,反复下载了很多遍也没有解决. 上网看过许多博客后,有好多方法,总结一下: ①在return 0:前加getchar():(getchar():是得到 ...
- Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)
题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...
- 总结在Linux终端中进行算术运算的6种方式
1.使用bash 使用双括号可以像C语言一样直接使用运算符进行计算. +)) a=$((*)) echo $a b=$(($a-)) echo $b d=$(($b/)) echo $d e=$(($ ...
- Python判断字符串是否全是字母或数字
str.isnumeric(): True if 只包含数字:otherwise False.注意:此函数只能用于unicode string str.isdigit(): True if 只包含数字 ...
- Jmeter使用基础笔记-写一个http请求
前言 本篇文章主要讲述2个部分: 搭建一个简单的测试环境 用Jmeter发送一个简单的http请求 搭建测试环境 编写flask代码(我参考了开源项目HttpRunner的测试服务器),将如下的代码保 ...