国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 852. Peak Index in a Mountain Array

ispring / 1046人閱讀

Problem

Let"s call an array A a mountain if the following properties hold:

A.length >= 3
There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

Example 1:

Input: [0,1,0]
Output: 1
Example 2:

Input: [0,2,1,0]
Output: 1
Note:

3 <= A.length <= 10000
0 <= A[i] <= 10^6
A is a mountain, as defined above.

Solution
class Solution {
    public int peakIndexInMountainArray(int[] A) {
        int start = 0, end = A.length-1;
        while (start+2 < end) {
            int mid = start+(end-start)/2;
            if (mid-1 >= start && mid+1 <= end && A[mid-1] < A[mid] && A[mid] > A[mid+1]) {
                return mid;
            } else if (mid-1 >= start && mid+1 <= end && A[mid-1] < A[mid] && A[mid] < A[mid+1]) {
                start = mid;
            } else if (mid-1 >= start && mid+1 <= end && A[mid-1] > A[mid] && A[mid] > A[mid+1]) {
                end = mid;
            } else {
                break;
            }
        }
        return start+1;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/72399.html

相關(guān)文章

  • Leetcode PHP題解--D12 852. Peak Index in a Mountain

    摘要:題目鏈接題目分析這個(gè)題目比較簡(jiǎn)單。要求返回?cái)?shù)組中最大值的索引。思路先用找到最大值,再用獲取最大值的索引。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 852. Peak Index in a Mountain Array 題目鏈接 852. Peak Index in a Mountain Array 題目分析 這個(gè)題目比較簡(jiǎn)單。要求返回?cái)?shù)組中最大值的索引。 思路 先用max找到最...

    luffyZh 評(píng)論0 收藏0
  • 9 .leetcode Peak Index in a Mountain Array

    摘要:題目例子我的解法其他解法求最大值然后求二分法查找 1 題目 Lets call an array A a mountain if the following properties hold: A.length >= 3There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[...

    txgcwm 評(píng)論0 收藏0
  • [Lintcode] Find Peak Element 找峰值

    摘要:找出該矩陣的一個(gè)峰值元素,返回他的坐標(biāo)原題鏈接一維二分搜索復(fù)雜度時(shí)間空間思路最直觀的方法是遍歷整個(gè)矩陣,但這要的時(shí)間。 Find Peak Element I A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], fi...

    leiyi 評(píng)論0 收藏0
  • [Leetcode] Find Minimum in Rotated Sorted Array 找旋

    摘要:二分迭代法復(fù)雜度時(shí)間空間遞歸棧空間思路找旋轉(zhuǎn)數(shù)組的起點(diǎn),實(shí)際上類似找一個(gè)山谷,只要兩邊都比中間高就對(duì)了,這和這題很像。 Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

    notebin 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<