Skip to content

Commit 215f7b8

Browse files
committed
添加 problem 852
1 parent 47e39d5 commit 215f7b8

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package leetcode
2+
3+
// 解法一 二分
4+
func peakIndexInMountainArray(A []int) int {
5+
low, high := 0, len(A)-1
6+
for low <= high {
7+
mid := low + (high-low)>>1
8+
if A[mid] > A[mid+1] && A[mid] > A[mid-1] {
9+
return mid
10+
}
11+
if A[mid] > A[mid+1] && A[mid] < A[mid-1] {
12+
high = mid - 1
13+
}
14+
if A[mid] < A[mid+1] && A[mid] > A[mid-1] {
15+
low = mid + 1
16+
}
17+
}
18+
return 0
19+
}
20+
21+
// 解法二 二分
22+
func peakIndexInMountainArray1(A []int) int {
23+
low, high := 0, len(A)-1
24+
for low < high {
25+
mid := low + (high-low)>>1
26+
// 如果 mid 较大,则左侧存在峰值,high = m,如果 mid + 1 较大,则右侧存在峰值,low = mid + 1
27+
if A[mid] > A[mid+1] {
28+
high = mid
29+
} else {
30+
low = mid + 1
31+
}
32+
}
33+
return low
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question852 struct {
9+
para852
10+
ans852
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para852 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans852 struct {
22+
one int
23+
}
24+
25+
func Test_Problem852(t *testing.T) {
26+
27+
qs := []question852{
28+
29+
question852{
30+
para852{[]int{0, 1, 0}},
31+
ans852{1},
32+
},
33+
34+
question852{
35+
para852{[]int{0, 2, 1, 0}},
36+
ans852{1},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 852------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans852, q.para852
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, peakIndexInMountainArray(p.one))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [852. Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)
2+
3+
4+
## 题目:
5+
6+
Let's call an array `A` a *mountain* if the following properties hold:
7+
8+
- `A.length >= 3`
9+
- 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]`
10+
11+
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]`.
12+
13+
**Example 1:**
14+
15+
Input: [0,1,0]
16+
Output: 1
17+
18+
**Example 2:**
19+
20+
Input: [0,2,1,0]
21+
Output: 1
22+
23+
**Note:**
24+
25+
1. `3 <= A.length <= 10000`
26+
2. `0 <= A[i] <= 10^6`
27+
3. A is a mountain, as defined above.
28+
29+
30+
## 题目大意
31+
32+
我们把符合下列属性的数组 A 称作山脉:
33+
34+
- A.length >= 3
35+
- 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
36+
给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。
37+
38+
提示:
39+
40+
- 3 <= A.length <= 10000
41+
- 0 <= A[i] <= 10^6
42+
- A 是如上定义的山脉
43+
44+
45+
46+
## 解题思路
47+
48+
- 给出一个数组,数组里面存在有且仅有一个“山峰”,(山峰的定义是,下标 `i``i-1``i+1` 位置上的元素都要大),找到这个“山峰”,并输出其中一个山峰的下标。
49+
- 这一题直接用二分搜索即可,数组中的元素算基本有序。判断是否为山峰的条件为比较左右两个数,如果当前的数比左右两个数都大,即找到了山峰。其他的情况都在山坡上。这一题有两种写法,第一种写法是标准的二分写法,第二种写法是变形的二分写法。

0 commit comments

Comments
 (0)