LeetCode 941. 有效的山脉数组(Valid Mountain Array)

941. 有效的山脉数组
941. Valid Mountain Array

题目描述
给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false。

让我们回顾一下,如果 A 满足下述条件,那么它是一个山脉数组:

  • A.length >= 3
  • 在 0 < i < A.length - 1 条件下,存在 i 使得:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[B.length - 1]

LeetCode941. Valid Mountain Array

示例 1:

输入: [2,1]
输出: false

示例 2:

输入: [3,5,5]
输出: false

示例 3:

输入: [0,3,2,1]
输出: true

提示:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000

Java 实现

class Solution {
    public boolean validMountainArray(int[] A) {
        if (A == null || A.length < 3) {
            return false;
        }
        int n = A.length;
        int low = 0, high = n - 1;
        while (low < high && A[low] < A[low + 1]) {
            low++;
        }
        while (low < high && A[high] < A[high - 1]) {
            high--;
        }
        if (low == n - 1 || high == 0) {
            return false;
        }
        return low == high ? true : false;
    }
}

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/10920674.html

时间: 2024-11-02 19:11:14

LeetCode 941. 有效的山脉数组(Valid Mountain Array)的相关文章

[Swift Weekly Contest 111]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 is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

leetCode 118. Pascal&#39;s Triangle 数组 (杨辉三角)

118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [      [1],     [1,1],    [1,2,1],   [1,3,3,1],  [1,4,6,4,1] ] 题目大意: 输入行数,输出如上图所示的数组.(杨辉三角) 思路: 用双vector来处理当前行和下一行. 代码如下: cla

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode 81——搜索旋转排序数组 II

1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1],[1, 1, 0, 1, 1],为了找到正确的转折点,我们查看 [mid, right] 之间有没有不等于 nums[mid] 的值,若有,则继续向右查找:否则向左查找. class Solution { public: int Binary_Search(vector<int>& num

Leetcode 448. Find All Numbers Disappeared in an Array

Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions Total Accepted: 31266 Total Submissions: 58997 Difficulty: Easy Contributors: yuhaowang001 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

C#+无unsafe的非托管大数组(large unmanaged array in c# without &#39;unsafe&#39; keyword)

C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一个大数组(Use a large array in C#) 在C#里,有时候我需要能够申请一个很大的数组.使用之.然后立即释放其占用的内存. Sometimes I need to allocate a large array, use it and then release its memory

加速数组操作(Array)

Measure-Command { $ar = @() for ($x=0; $x -lt 10000; $x++) { $ar += $x } }执行结果:3.301s Measure-Command { $ar = New-Object -TypeName System.Collections.ArrayList for ($x=0; $x -lt 10000; $x++) { $ar.Add($x) } }执行结果:0.047s From:http://powershell.com/cs/