LeetCode: 852. Peak Index in a Mountain Array

題目:

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].

範例:

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

這題是要找出山峰的位置
我解的時候
是判斷要與left、right的點作比較
但後來看到其它人解法
其實只要判斷第一個下降的位置就好了
看來想法還是不到位

解法:

func peakIndexInMountainArray(A []int) int {
    var index int
    for i := 1; i < len(A)-1; i++{
        if A[i-1] < A[i] && A[i] > A[i+1] {
            index = i
            break
        }
    }
    return index
}

This Post Has 6 Comments

  1. Justin

    Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?
    (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like yours– but the theme slows (!) the
    site down quite a bit.

    In case you have a minute, you can find it by searching
    for “royal cbd” on Google (would appreciate any
    feedback) – it’s still in the works.

    Keep up the good work– and hope you all take care of yourself during the coronavirus scare!

    1. jirongwu81@gmail.com

      Just used OceanWP, I did not modify anything.
      Maybe you should use some plugin to boost webpage loading,like Fast Velocity Minify, WP Super Cache and so on.
      This website build in SiteGround, it has the SG Optimizer plugin.

      Thanks for your visting.

  2. RogerCeawl

    Hello

    1. jirongwu81@gmail.com

      Hi~

  3. xbetldevvz

    very interesting but nothing is clear
    _________________
    sГјperbetting Еџikayet var [url=https://sport.azavbioham.ml/1x_bet.html]1xBet[/url] betnow location

    1. jirongwu81@gmail.com

      Thanks a lot, I would change this problem in the future。

發佈留言