LeetCode.941-有效山形数组(Valid Mountain Array)
这是悦乐书的第360次更新,第387篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第222题(顺位题号是941)。给定一个整数数组A,当且仅当它是一个有效的山形数组时返回true。
A是一个山形数组,当且仅当:
A.length> = 3
存在一些具有0 < i < A.length-1的i,使得:
A[0] < A[1] < ... < A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length-1]
例如:
输入:[2,1]
输出:false
输入:[3,5,5]
输出:false
输入:[0,3,2,1]
输出:true
注意:
0 <= A.length <= 10000
0 <= A [i] <= 10000
02 解题
根据题目的规则,要满足山形数组的条件,不仅数组元素个数要大于等于三个,并且要存在一个值,使得在这个值之前的元素是递增形势,在这个值之后的元素是递减形势。
从数组第一个元素开始遍历,做递增判断,直到不满足条件为止,经历递增的判断后,需要判断索引是否位于中间,即不能是0或者是最后一个,再继续递减的判断,判断结束后,判断索引是否等于最后一位即可。
public boolean validMountainArray(int[] A) {
if (A == null || A.length < 3) {
return false;
}
int index = 0, n = A.length-1;
while (index < n && A[index] < A[index+1]) {
index++;
}
// 如果A是单调递增或者单调递减,直接返回false
if (index == 0 || index == n) {
return false;
}
while (index < n && A[index] > A[index+1]) {
index++;
}
return index == n;
}
03 小结
算法专题目前已连续日更超过七个月,算法题文章228+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.941-有效山形数组(Valid Mountain Array)的更多相关文章
- 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 ...
- 【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_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- 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 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...
随机推荐
- C++构造函数实例
#include<iostream> #include <string> using namespace std; class Person { public: //无参(默认 ...
- Python 面向对象Ⅴ
基础重载方法 下表列出了一些通用的功能,你可以在自己的类重写: 运算符重载 Python同样支持运算符重载,实例如下: 以上代码执行结果如下所示: 类属性与方法 类的私有属性 __private_at ...
- Python 异常处理Ⅱ
异常处理 捕捉异常可以使用try/except语句. try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理. 如果你不想在异常发生时结束你的程序,只需在try ...
- C# 扩展方法——序列化与反序列化
其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html 主要是是对日期格式的处理 using Newtonsoft.Json; using ...
- javascript中的原型和原型链(四)
new运算符工作原理
- Flyway Validate failed: Migration checksum mismatch for migration version 1.0.0.01 错误
在运行系统的时候出现错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ...
- sh_02_del关键字
sh_02_del关键字 name_list = ["张三", "李四", "王五"] # (知道)使用 del 关键字(delete)删除 ...
- 安装python第三方模块
下载 第三方模块的下载地址:https://pypi.python.org/pypi 其他版本的第三方模块下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs ...
- openvas 安装
NMAP apt-get update & apt-get upgrade kali的更新命令 https://www.fujieace.com/kali-linux/update-sourc ...
- 构建springboot的几种方式 在线构建 STS构建 Idea 内置构建 Maven 构建
SpringBoot项目的几种创建方式,启动.和访问 最常用的4种方式,但除了这些以外,还有其他方式: ①在线创建 ②STS构建 ③Intell Idea内置构建工具 ④Maven创建 STS官 ...