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>& ...
随机推荐
- node-rsa
[node-rsa] 引用 var NodeRSA = require('node-rsa') 生成一个私钥长度为512的key(同时生成公钥) var key = new NodeRSA({b: 5 ...
- BOM 对象--location、navigator、screen、history
1.location 对象 location提供了与当前窗口中加载的文档有关的信息,还有一些导航功能.需要注意的是,window.location 和 document.location 引用的是同一 ...
- 无线渗透wep加密路由器
停掉网络服务 service network-manager stop 检查现在的环境适不适合使用airmon-ng airmon-ng check 杀死可能冲突的进程 开启网卡monitor模式 a ...
- python全栈开发 什么是python python命名及循环
python全栈 一. python介绍: 1. python起源 2. 主要应用领域; web,人工智能,云计算,系统运维. 1.1 python是一门什么语言? python是一 ...
- oracle中的trim()函数详解
1.先看一下Oracle TRIM函数的完整语法描述 TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} ...
- WOPI的安装文档方法
Office Web Apps安装部署 系统要求为Windows Server 2012, 注意:安装Office Web Apps的服务器除了Office Web Apps之外,不能安装其他应用.包 ...
- 替换空格(python)
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. # -*- coding:ut ...
- Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等
Selenium 如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...
- Angular之替换根组件
一 在index.html中,替换根组件选择器 <!doctype html> <html lang="en"> <head> <meta ...
- matlab基础绘图知识
axis([xmin xmax ymin ymax]) %设置坐标轴的最小最大值 xlabel('string') %标记横坐标 ylabe ...