【Leetcode_easy】941. Valid Mountain Array
problem
solution:
class Solution {
public:
bool validMountainArray(vector<int>& A) {
if(A.size()<) return false;
int max = A[], max_id = ;
for(int i=; i<A.size(); ++i)
{
if(A[i]>max)
{
max = A[i];
max_id = i;
}
}
if(max_id== || max_id==A.size()-) return false;//errr..
for(int i=; i<A.size(); i++)
{
if(i<max_id && A[i]<=A[i-]) return false;//errr...
if(i>max_id && A[i]>=A[i-]) return false;//errr..
}
return true;
}
};
solution2:
class Solution {
public:
bool validMountainArray(vector<int>& A) {
int i=, n=A.size()-, j=n;
while(i<n- && A[i]<A[i+]) i++;
while(j> && A[j-]>A[j]) j--;
return i> && i==j && j<n;
}
};
参考
1. Leetcode_easy_941. Valid Mountain Array;
完
【Leetcode_easy】941. Valid Mountain Array的更多相关文章
- 【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_easy】1037. Valid Boomerang
problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完
- 【Leetcode_easy】1122. Relative Sort Array
problem 1122. Relative Sort Array 参考 1. Leetcode_easy_1122. Relative Sort Array; 2. helloacm; 完
- 【Leetcode_easy】680. Valid Palindrome II
problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...
- LeetCode 941. 有效的山脉数组(Valid Mountain Array)
941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- [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 ...
随机推荐
- 转发大神nginx配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- info命令
info命令也可以查看命令的信息,但是和man命令不同,info命令如同一本书,一个命令只不过是书中的一个章节. 1.基本结构: 输入:info ls ls命令只不过是整个文档的10.1章节. 而在章 ...
- PHP Socket 编程之9个主要函数的使用之测试案例
php的socket编程算是比较难以理解的东西吧,不过,我们只要理解socket几个函数之间的关系,以及它们所扮演的角色,那么理解起来应该不是很难了,在笔者看来,socket编程,其实就是建立一个网络 ...
- Windows下 Python 2 与 Python 3 共存
转自:http://lovenight.github.io/2016/09/27/Windows%E4%B8%8B-Python-2-%E4%B8%8E-Python-3-%E5%85%B1%E5%A ...
- mobx中的数组需要注意的地方
mobx中如果将数组作为可观察. 可以通过添加修饰符observable或者调用observable方法. 很多的时候, 我们将此修饰为可观察的对象后, 就随处可用了. 比如,采用 map forE ...
- php-fpm 参数调优
php-fpm 进程池优化方法 php-fpm进程池开启进程有两种方式,一种是static,直接开启指定数量的php-fpm进程,不再增加或者减少:另一种则是dynamic,开始时开启一定数量的php ...
- flutter 省市区选择器 city_pickers 的简单实用
Github地址:https://github.com/hanxu317317/city_pickers packages地址: https://pub.flutter-io.cn/packages/ ...
- gitconfig别名配置
vim ~/.gitconfig 进行配置 [user] name = Your Name email = you@yourdomain.example.com [core] editor = vim ...
- mysql 连接服务器报ERROR 1130 ,mysql服务正常运行
今天办公电脑的mysql罢工了,折腾了半天,记录一下. 错误如下: ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to ...
- java 跳出多重循环
public class Main { public static void main(String[] args) { System.out.println("start"); ...