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 is a mountain array if and only if:
A.length >= 3
- There exists some
i
with0 < i < A.length - 1
such that:A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[B.length - 1]
Example 1:
Input: [2,1]
Output: false
Example 2:
Input: [3,5,5]
Output: false
Example 3:
Input: [0,3,2,1]
Output: true
Note:
0 <= A.length <= 10000
0 <= A[i] <= 10000
Approach #1:
class Solution {
public:
bool validMountainArray(vector<int>& A) {
int len = A.size();
if (len < 3) return false;
auto index = max_element(A.begin(), A.end()) - A.begin();
if (index == len - 1 || index == 0) return false;
for (int i = 0; i < index; ++i)
if (A[i] >= A[i+1]) return false;
for (int i = index; i < len-1; ++i)
if (A[i] <= A[i+1]) return false;
return true;
}
};
Weekly Contest 111-------->941. Valid Mountain Array(max_element)的更多相关文章
- 【Leetcode_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...
- 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 ...
- 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique
Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
随机推荐
- 通过串口工具下发指令的Python脚本
前言 最近一段时间在测试物联网相关的App自动化,涉及通过串口工具给硬件设备下发指令. 使用的串口工具:SecureCRT 解决办法 通过引用Python的第三方库:serial,通过编写Python ...
- Java类加载器(死磕3)
[正文]Java类加载器( CLassLoader ) 死磕3: 揭秘 ClassLoader抽象基类 本小节目录 3.1. 类的加载分类:隐式加载和显示加载 3.2. 加载一个类的五步工作 3. ...
- Chef vs Puppet vs Ansible vs Saltstack: Which Works Best For You?
Ansible vs SaltStack 谁才是自动化运维好帮手? - CSDN博客 https://blog.csdn.net/a105421548/article/details/53558598 ...
- ABAP 通过字段找表程序
2.获取数据保存在哪个数据表的方法: 1.前台对指定栏位 使用F1帮助找表,2.st05 跟踪业务操作过程,检索需要的数据表,(此方法找表很高效)3.对于文本字段找表,可以找到前台维护处,->维 ...
- return;测试
一 没有return;,则会顺序执行到最后 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- require实现单页应用程序(SPA)
写了一个测试代码,用require.js配合它的一个插件text.js实现了最简单的单页应用程序,简单的记录一下,方便以后复习, git地址:https://github.com/lily1010/r ...
- IE浏览器没有加载CSS或js文件的秘密及解决办法
其实是两处资料拼成这一篇博文的,因为在开发过程中遇到,有的文章只是说明原因,而没有给出解决方案,所以再次给出解释和解决方法,以供参考,如果有好的解决方法,也请分享下! ---------------- ...
- ffmpeg 的一些学习网站
http://blog.csdn.net/leixiaohua1020/article/category/1360795 雷霄骅(leixiaohua1020)的专栏 http://dranger.c ...
- c++变量定义
float **a 表示a是一个“指针的指针”,也可以理解为是一个二维数组的指针,***a具有类似的解释,可以理解为是一个三维数组的指针.
- html5--3.19 新增的progress/meter元素
html5--3.19 新增的progress/meter元素 学习要点 了解progress/meter元素的用法 progress元素 是HTML5中新增的元素,用来建立一个进度条 通常与Java ...