Skip to content

Latest commit

 

History

History

852.山脉数组的峰顶索引

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

852. 山脉数组的峰顶索引

我们把符合下列属性的数组 A 称作山脉:

  • A.length >= 3
  • 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。

示例 1:

输入:[0,1,0]
输出:1

示例 2:

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

提示:

1. 3 <= A.length <= 10000
2. 0 <= A[i] <= 10^6
3. A 是如上定义的山脉
func peakIndexInMountainArray(A []int) int {
}

解题思路

二分查找,一直查询直到start+1,end相遇

题解

func peakIndexInMountainArray(A []int) int {
    
    start := 0
    end := len(A) - 1
    
    for start + 1 < end {
        mid := start + (end - start) / 2
        if A[mid] > A[mid+1] {
            end = mid
        } else {
            start = mid
        }
    }
    
    if A[start] > A[end] {
        return start
    }
    return end
}