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 with 0 < 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:

  1. 0 <= A.length <= 10000
  2. 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)的更多相关文章

  1. 【Leetcode_easy】941. Valid Mountain Array

    problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...

  2. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  3. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode 941. Valid Mountain Array (有效的山脉数组)

    题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...

  5. LeetCode 941. 有效的山脉数组(Valid Mountain Array)

    941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

随机推荐

  1. RabbitMQ安装和介绍

    简单的安装方式 yum安装erlang,下载rpm包安装rabbitmq 一.编译安装erlang 1. 官方下载包并解压 wget http://erlang.org/download/otp_sr ...

  2. Hadoop集群_Hadoop安装配置

    1.集群部署介绍 1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesy ...

  3. ByteBuf(图解1)

    目录 源码工程 写在前面 Netty ByteBuf 优势 手动获取与释放ByteBuf 自动获取和释放 ByteBuf 方式一:TailHandler 自动释放 方式二:SimpleChannelI ...

  4. Part of defining a topology is specifying for each bolt which streams it should receive as input

    http://storm.apache.org/ [doing for realtime processing what Hadoop did for batch processing ] Apach ...

  5. eclipse集成SVN插件-----复制添加插件

    首先从网上下载一个svn的插件包, 将插件包解压, 打开eclipse的文件夹, 将svn插件中features文件夹中的jar复制到eclipse中features文件夹中去: 将svn插件中plu ...

  6. wireshark 学习 2

    使用wireshark抓到的wifi数据包如果是加密的,就只能显示密文,无法得到真正的数据. 如果知道AP和SSID和key,就可以解密wifi数据包,显示上层协议的数据. 在wireshark中设置 ...

  7. Redis使用经验之谈

    应用场景 保存用户喜欢的商品信息. 类型: Hash, key: usr:${type_id}:${version_id}:${user_id}:${warehouse_id}, field: ${s ...

  8. HTML(DOM)与JavaScript嵌套数组之间相互转换

    html2ja:将html目标元素解析为JavaScript数组字面量,每项的值为tagName, className, id等CSS选择器组合: showJa:将html2ja生成的数组缩进格式化显 ...

  9. struts2中<s:if>标签的使用

    转载:http://blog.sina.com.cn/s/blog_5f9938640100v2kr.html A:<s:if>判断字符串的问题: 1.判断单个字符:<s:if te ...

  10. OJ提交题目中的语言选项里G++与C++的区别

    一.OJ提交题目中的语言选项里G++与C++的区别 http://www.th7.cn/Program/cp/201405/199001.shtml 首先更正一个概念,C++是一门计算机编程语言,G+ ...