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
i
with0 < i < A.length - 1
such 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 <= 10000
0 <= 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>& ...
随机推荐
- 关于OPEN_MAX宏undeclared的问题
最近在看unp时,I/O复用-poll一章的代码使用到了OPEN_MAX.据书中描述,这一宏定义在limits.h头文件中,指代一个进程在任意时刻能打开的最大描述符数目.但在代码编译时遇到了错误,提示 ...
- 【Django】ModuleNotFoundError: No module named 'books_ordersschool'
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000 ...
- 【pycharm】使用过程的相关问题
背景:安装scrapy后在cmd里可以正常import scrapy模块,但是在pycharm里不可以(python2.7) 问题:cmd中能正常导入模块,在pycharm报错 原因:pycharm里 ...
- 区分action, service和dao
在这里,我们不仅要处理页面逻辑,还要做业务逻辑,还要做数据访问.比如说: public String addUser() { if(user == null) { return FAIL_NO_USE ...
- cef研究
// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights // reserved. Use of this s ...
- java中替换${xx}
import java.util.regex.Matcher; import java.util.regex.Pattern; public class replace { public static ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- 初识Netty
我们已经了解了Socket通信/IO/NIO/AIO编程,对于通信模型已经有了一个初步的认识,其实我们之前所学习的仅仅是一个模型,如果想把这些真正的用于实际工作中去,其实我们之前所学习的仅仅是一个模型 ...
- Python: 浅淡Python中的属性(property)
起源:项目过程中需要研究youtube_dl这个开源组件,翻阅其中对类的使用,对比c#及Delphi中实现,感觉Python属性机制挺有意思.区别与高级编程语言之单一入口,在类之属性这一方面,它随意的 ...
- iOS - 常用本机URL跳转设置
import UIKit class ViewController: UIViewController { override func touchesBegan(_ touches: Set<U ...