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 is a mountain array if and only if:
A.length >= 3- There exists some
iwith0 < i < A.length - 1such 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:
0 <= A.length <= 100000 <= A[i] <= 10000
Idea 1. 遍历验证是否up-down once
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public boolean validMountainArray(int[] A) {
int n = A.length;
// if(n < 3) {
// return false;
// }
int left = 0;
while(left+1 < n && A[left] < A[left+1]) {
++left;
}
if(left == 0 || left == n-1) {
return false;
}
while(left+1 < n && A[left] > A[left+1]) {
++left;
}
if(left == n-1) {
return true;
}
return false;
}
}
Idea 1. 网上看到的有意思的解法,both two men walking up from the bottom
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public boolean validMountainArray(int[] A) {
int n = A.length;
int left = 0;
while(left+1 < n && A[left] < A[left+1]) {
++left;
}
int right = n-1;
while(right - 1 >= 0 && A[right-1] > A[right]) {
--right;
}
return left == right && left!= 0 && right != n-1;
}
}
Valid Mountain Array LT941的更多相关文章
- [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 ...
- Weekly Contest 111-------->941. Valid Mountain Array(max_element)
Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...
- 【Leetcode_easy】941. Valid Mountain Array
problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...
- LeetCode 941. 有效的山脉数组(Valid Mountain Array)
941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...
- 【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.941-有效山形数组(Valid Mountain Array)
这是悦乐书的第360次更新,第387篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第222题(顺位题号是941).给定一个整数数组A,当且仅当它是一个有效的山形数组时返回 ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
随机推荐
- 第三篇:jmeter的作用域和执行顺序
1.元件的作用域: 8类可执行的元件,testplan和threadgroup不属于可执行的元件:这些元件中,取样器,是典型的不与其他元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其他 ...
- NumPy 副本和视图
NumPy 副本和视图 副本是一个数据的完整的拷贝,如果我们对副本进行修改,它不会影响到原始数据,物理内存不在同一位置. 视图是数据的一个别称或引用,通过该别称或引用亦便可访问.操作原有数据,但原有数 ...
- mysql windows安装资源
压缩包资源 https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/ 配置流程 https://blog.csdn.net/hel ...
- 关于后台执行JS代码总结
方法一.FineUI的 pagecontext对象 string js="functionName();"; PageContext.RegisterStartUpScript(j ...
- python 库安装方法及常用库
python 库安装方法及常用库 python库安装方法: 方法一:setpu.py 1.下载库压缩包,解压,记录下路径:*:/**/……/ 2.运行cmd,切换到*:/**/……/目录下 3.运行s ...
- 成对使用new和delete,传值传引用
首先: delete []p;是用来删除对象数组的,特别是你声明的是对象数组!!! 如果new中用了[],delete一定要用[]:在new中没有使用,在delete中一定不要使用. 其次: 当你使用 ...
- 使用HttpClient 传送form 表单的请求
在项目中用到了,需要使用HttpClient 进行模拟表单传送form 表单的需求,在平常的项目中,大概都是传送json串的样式需求,但是如何才能给对应的服务器传送一个form 表单呢? 这就需要了N ...
- Head First Servlets & JSP 学习笔记 第三章 —— MVC迷你教程
临渊羡鱼,不如退而结网!是时候动手搞事情了! 我们的四大步骤: ①分析用户的视图(也就是浏览器要显示的东西),以及高层体系结构: (这个就是所谓的前端吧?用JSP?JSP可以当成Html来用吧?高层体 ...
- mysql 主从数据不一致 Slave_SQL_Running: No 解决方法
在slave服务器上通过如下命令 mysql> show slave status\G; 显示如下情况: Slave_IO_Running: Yes Slave_SQL_Running: No ...
- .net项目错误:找不到方法:“System.Net.Http.HttpClient stellar_dotnet_sdk.Server.get_HttpClient()
1.由于在项目里面引用了一个 新的项目stellar_dotnet_sdk,在 return new StellarWallet(ConvertToWalletSetting(coin)); 的 ...