【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 ...
随机推荐
- D. Vasya and Triangle(思维, 三角形)
传送门 题意: 给你 n, m, k, 问你是否存在一个三角形, 满足三角形的面积等于 n * m / k: 若存在, 输出YES, 且输出满足条件的三角形的三个坐标(答案有多种,则输出任意一种) ...
- 利用fgetc合并2个源文件的内容,到一个新的文件中
#include <stdio.h> #include <stdlib.h> //功能: 合并2个源文件的内容,到一个新的文件中 int main(int a,char *ar ...
- golang-切片
切片练习 package main import "fmt" /*func main() { arr := [10]int {1, 2, 3, 4, 5, 6, 7, 8, 9, ...
- 关于nginx反代jenkins报错 反向代理设置有误
官方文档地址: https://wiki.jenkins.io/display/JENKINS/Running+Jenkins+behind+Nginx 直接解决的配置文件吧. 这是使用子域名,不使用 ...
- [ZJOI2007][BZOJ1060]时态同步
Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路 ...
- 模板 - 数据结构 - 链表/LinkedList
一个只供删除的双向链表,为了简单不再引入head节点,而且也不进行next的套娃操作.空间使用略微多了一些,但是无伤大雅. struct LinkedList { static const int M ...
- archlinux安装nvidia-1050ti闭源驱动教程,亲测
link:https://blog.csdn.net/u014025444/article/details/91454059
- Linux 文件与目录的权限
文件默认权限:umask umask就是指定“目前用户在新建文件或目录时候的权限默认值”.查询umask有以下两种方式: 目录与文件的默认权限是不一样的: - 文件,默认没有可执行(x)权限,只有r. ...
- 【Python 代码】生成hdf5文件
import random from PIL import Image import numpy as np import os import h5py from PIL import Image L ...
- SQLEXPR_x64_CHS、SQLEXPRADV_x64_CHS、SQLEXPRWT_x64_CHS、SqlLocalDB、SQLManagementStudio_x64_CHS各版本说明
LocalDB (SqlLocalDB)LocalDB 是 Express的一种轻型版本,该版本具备所有可编程性功能,但在用户模式下运行,并且具有快速的零配置安装和必备组件要求较少的特点.如果您需要通 ...