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 ...
随机推荐
- Microsoft SQL Server学习(七)--函数视图
系统函数 视图 索引 1.系统函数 (1) ()数学函数 Abs() 绝对值 Floor() 向下取整 Ceiling() 向上取整 Sin() 返回指定角度(以弧度为单位)的三角正弦值 Pi() 圆 ...
- Sqoop hive 和mysql 交互 完整案例
本文完成:在hive里建管理表:注入部分数据:利用sqoop导入mysql中 期间:解决中文乱码问题 飞行报告故障表 建表命令 查看表 人工灌入少量数据 Windows系统向Linux系统数据传输 ...
- Android显示相册图片和相机拍照
首先看最重要的MainActive类: public class MainActivity extends AppCompatActivity { private final int FROM_ALB ...
- Application received signal SIGSEGV
Application received signal SIGSEGV (null) (( 0 CoreFoundation 0x0000000181037d50 <redacted> + ...
- vue组件---插槽
(1)插槽内容 Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 <slot> 元素作为承载分发内容的出口. 在父级组件里可以 ...
- react antD moment
import moment from 'moment' console.log(moment().add(1, 'days').format('YYYY-MM-DD')) //当前时间前一天 cons ...
- 学习SpringBoot中遇见的坑
1. 在搭建SpringBoot HelloWorld 时项目结构应该这样: 而不能这样: 否则访问时出现错误页面: 原因:此时还不知道,先记录下来. --已解决2018/12/11,因为Spring ...
- [UVA11825]Hackers' Crackdown(状压dp)
题解降智警告 吐槽降智警告 思路降智警告 代码降智警告 题目传送门 洛谷 果然水题做多了连半道难点的都能给咱干蒙... 水题做多了降智 --鲁迅 题目大意:见传送门 心路历程见末尾 正解(大概): ...
- mysql的密码管理、mysql初始密码查找、密码修改、mysql登录
1.查询mysql的初始密码: 初始密码密码是随机产生的,每台机器产生的都不一样的 grep 'temporary password' /var/log/mysqld.log 或者 cat /var/ ...
- python3.x Day3 文件操作
文件操作:操作文件实际是4步骤1.描述文件是哪个 2.打开文件 3.操作文件 4.关闭文件 1.打开文件使用open方法,代码举例: data=open("wait_you",en ...