problem

941. Valid Mountain Array

solution:

  1. class Solution {
  2. public:
  3. bool validMountainArray(vector<int>& A) {
  4. if(A.size()<) return false;
  5. int max = A[], max_id = ;
  6. for(int i=; i<A.size(); ++i)
  7. {
  8. if(A[i]>max)
  9. {
  10. max = A[i];
  11. max_id = i;
  12. }
  13. }
  14. if(max_id== || max_id==A.size()-) return false;//errr..
  15. for(int i=; i<A.size(); i++)
  16. {
  17. if(i<max_id && A[i]<=A[i-]) return false;//errr...
  18. if(i>max_id && A[i]>=A[i-]) return false;//errr..
  19. }
  20. return true;
  21. }
  22. };

solution2:

  1. class Solution {
  2. public:
  3. bool validMountainArray(vector<int>& A) {
  4. int i=, n=A.size()-, j=n;
  5. while(i<n- && A[i]<A[i+]) i++;
  6. while(j> && A[j-]>A[j]) j--;
  7. return i> && i==j && j<n;
  8. }
  9. };

参考

1. Leetcode_easy_941. Valid Mountain Array;

2. discuss_climb_mountain;

【Leetcode_easy】941. Valid Mountain Array的更多相关文章

  1. 【leetcode】941. Valid Mountain Array

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

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

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

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

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

  4. 【Leetcode_easy】1037. Valid Boomerang

    problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完

  5. 【Leetcode_easy】1122. Relative Sort Array

    problem 1122. Relative Sort Array 参考 1. Leetcode_easy_1122. Relative Sort Array; 2. helloacm; 完

  6. 【Leetcode_easy】680. Valid Palindrome II

    problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...

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

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

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

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

随机推荐

  1. Set的常用实现类HashSet和TreeSet

    Set HashSet public static void main(String[] args) { //不可以重复  并且是无序的  //自然排序  从A-Z  //eqauls从Object继 ...

  2. thinkpadT470P安装问题

    [问题描述]: 最近在将Thinkpad E430c的ubuntu系统重装成windows 7的过程中,出现了装好win7系统后,开机自动进入boot menu界面的问题,而且不论你选择从光驱还是硬盘 ...

  3. django 第三天 视图

    今日内容 一.url路由分发之include 项目文件夹下的urls.py文件中的url写法: from django.conf.urls import url,include from django ...

  4. 【Redis】Linux下Redis的安装

    Redis服务安装 主要有两种方式:apt安装和编译安装. 我采用的是apt安装,系统是ubuntu18.04,Redis version 5:4.0.9-1 在 Ubuntu 系统安装 Redi 可 ...

  5. SaltStack 在 Windows 上的操作基础

    SaltStack 在 windows上的操作基础 1.删除文件: salt '172.16.3.11' file.remove 'D:\downup\111.msu' 2.删除文件夹 salt '1 ...

  6. 关于Keil4 转到 Keil5以后的一些错误解决

    一, 看自己选择CPU型号,根据型号再做配置 根据自己型号填写

  7. leetcode解题报告(32):Teemo Attacking

    描述 In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poison ...

  8. Ural1297 最长回文子串(后缀数组+RMQ)

    /* 源程序丢失QWQ. 就不粘代码了. 大体做法是把串反转然后连接. 做一遍后缀数组. 对height做一遍rmq. 然后对于每个位置的奇偶分别判断, 记下pos. 注意求的是[l+1,r]的hei ...

  9. 洛谷 P1230 智力大冲浪 题解

    P1230 智力大冲浪 题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者 \(m\)元.先不要太高兴!因为这些钱还不一定都是你的 ...

  10. 机器学习实战(1)- KNN

    KNN:k近邻算法-在训练样本中找到与待测样本距离相近的N个样本,并用这N个样本中所属概率最大的类别作为待测样本的类别. 算法步骤: 1.对训练中的样本数据的不同属性进行归一化处理. 2.计算待测样本 ...