[LeetCode] 1095. Find in Mountain Array

山脉数组中查找目标。做这个题如果对山脉数组的定义不是很明确,可以先做一下852题。这个题可以跟162,852一起做。山脉数组基本上就是只有一个峰值peak,峰值左边的元素是单调递增的,右边的元素是单调递减的。本题不允许你直接访问input数组,你只能通过length()函数知道数组的长度,通过get()函数得知某一个数字的值。例子,

Example 1:

Input: array = [1,2,3,4,5,3,1], target = 3
Output: 2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.

Example 2:

Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist in the array, so we return -1.

这个题的思路会用到三次二分法,第一次帮助找到peak元素,第二次去找peak元素左半边是否存在target,第三次在peak元素右半边查找target。

时间O(logn)

空间O(1)

Java实现

 1 /**
 2  * // This is MountainArray's API interface.
 3  * // You should not implement it, or speculate about its implementation
 4  * interface MountainArray {
 5  *     public int get(int index) {}
 6  *     public int length() {}
 7  * }
 8  */
 9  
10 class Solution {
11     public int findInMountainArray(int target, MountainArray mountainArr) {
12         int n = mountainArr.length();
13         int peak = 0;
14         int mid = 0;
15         // find index of peak
16         int left = 0;
17         int right = n - 1;
18         while (left < right) {
19             mid = left + (right - left) / 2;
20             if (mountainArr.get(mid) < mountainArr.get(mid + 1)) {
21                 left = peak = mid + 1;
22             } else {
23                 right = mid;
24             }
25         }
26 
27         // find target in the left of peak
28         left = 0;
29         right = peak;
30         while (left <= right) {
31             mid = left + (right - left) / 2;
32             if (mountainArr.get(mid) < target) {
33                 left = mid + 1;
34             } else if (mountainArr.get(mid) > target) {
35                 right = mid - 1;
36             } else {
37                 return mid;
38             }
39         }
40 
41         // find target in the right of peak
42         left = peak;
43         right = n - 1;
44         while (left <= right) {
45             mid = left + (right - left) / 2;
46             if (mountainArr.get(mid) > target) {
47                 left = mid + 1;
48             } else if (mountainArr.get(mid) < target) {
49                 right = mid - 1;
50             } else {
51                 return mid;
52             }
53         }
54         return -1;
55     }
56 }

猜你喜欢

转载自www.cnblogs.com/aaronliu1991/p/12799822.html