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>& ...
随机推荐
- springcloud eureka.instance
1.在springcloud中服务的 Instance ID 默认值是: ${spring.cloud.client.hostname}:${spring.application.name}:${sp ...
- GreenDao在列中的单词之间自动加_
1.第一种情况,原字段(属性.列)是 驼峰式命名法 @Entitypublic class Employee { @Id(autoincrement = true) private Long id; ...
- vi 基本命令使用
vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对 Unix及Linux系统的任何版本,vi编辑器是完全 ...
- hibench 对CDH5.13.1进行基准测试(测试项目hadoop\spark\)HDFS作HA高可靠性
使用CDH 5.13.1部署了HADOOP集群之后,需要进行基准性能测试. 一.hibench 安装 1.安装位置要求. 因为是全量安装,其中有SPARK的测试(SPARK2.0). 安装位置在SPA ...
- 51nod 1163 最高的奖励
链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163 1163 最高的奖励 基准时间限制:1 秒 空间限制:13 ...
- slf4j + log4j 需要的依赖
正确的依赖 <!-- slf4j 依赖包 --> <dependency> <groupId>org.slf4j</groupId> <artif ...
- 高德地图开发者平台获取sHA1值
一般在 Application 中进行初始化 /** * 获取高德SHA1值 * */ public static String sHA1(Context context) { try { Pack ...
- cgi,fast-cgi,php-cgi,php-fpm转载详解
转载自:https://segmentfault.com/q/1010000000256516 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编 ...
- Redis 中 redis.conf配置详细解析
########################################### 基本配置 ##################################### # 端口 port 666 ...
- solrj 测试连接 6.6.5solr集群
我开始环境是 linux上是6.6.5 pom也是6.6.5 按照学习视频的demo,他用的是4点几的solr,我换成了6点几的,没有CloudSolrServer 只有CloudSolrClie ...