【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 ...
随机推荐
- 从零开始开发一个Spring Boot Starter
一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...
- linux 环境变量的设置
方法一: 在/etc/profile文件中添加变量[对所有用户生效(永久的)] 用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”. 要让刚才的 ...
- printf的使用
#!/bin/bashprintf "|------------------------------------\n"printf "this is printf str ...
- shell脚本之字符串运算的使用
字符串运算符 下表列出了常用的字符串运算符,假定变量 a 为 "abc",变量 b 为 "efg": 运算符 说明 举例 = 检测两个字符串是否相等,相等返回 ...
- C语言函数的定义和使用(2)
一:无参函数 类型说明符 get(){ //函数体 } 二:无参函数 类型说明符 getname(int a,int b){ //函数体 } 三:类型说明符包括: int ,char,float,do ...
- 课标2-2-1-3 :MMU配置与使用
void create_page_table(void){ unsigned long *ttb = (unsigned long *)0x20000000; unsigned long vaddr, ...
- AtCoder Beginner Contest 133 E - Virus Tree 2(组合数学)
题意 n个点的树k种颜色,距离不超过2的点对需颜色不同,求方案数 Code(copy) #include<iostream> #include<cstdio> #include ...
- Java 操作Redis封装RedisTemplate工具类
package com.example.redisdistlock.util; import org.springframework.beans.factory.annotation.Autowire ...
- PostgreSQL: Rename a User
Syntax The syntax to rename a user using the ALTER USER statement in PostgreSQL is: ALTER USER user_ ...
- go语言读写文件
package main import ( "fmt" "io/ioutil" "os" ) func main() { filename ...