【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题记录
问题描述
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:
[4,5,6,7,0,1,4] if it was rotated 4 times.
[0,1,4,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n-2]].
Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.
You must decrease the overall operation steps as much as possible.
测试样例
1 | Input: nums = [1,3,5] |
1 | Input: nums = [2,2,2,0,1] |
说明
1 | n == nums.length |
解题
思路
由于数组旋转一次,则从最小值所在的位开始,数组无序,且这个节点之前或之后的子数组都是有序的。
使用二分法,判断中点前后是否有序来决定在哪一侧寻找结果:
- 若中点小于最右侧点,则右侧必有序,在左侧寻找(可能中点为目标)
- 若中点大于最右侧点,则右侧必无序,在右侧寻找
- 若中点等于最右侧点,无法判断哪侧有序,右指针左移(最右侧点和中点相同,保留一个即可,右侧点可略过)
判断中点及最右侧点,若数组未旋转,最终会找到首位;若判断左侧点和中点则会找到末尾。
补充:
- 二分法
- 时间复杂度
O(lg(n))
代码
1 | class Solution { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哆啦 C 梦!
评论