[LeetCode]154.Find Minimum in Rotated Sorted Array II

简介:

【题目】

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.


【分析】

这一道题是上一道题的拓展延伸:[LeetCode]153.Find Minimum in Rotated Sorted Array

上一道题目说明没有重复数字,这一道题目添加上了这一要求,有了重复数字。

因此这一道题目比上一道题目多了些特殊情况:

我们看一组例子:{1,0,11,1} 和 {1,1,1,0,1} 都可以看成是递增排序数组{0,1,1,1,1}的旋转。

这种情况下我们无法继续用上一道题目的解法,去解决这道题目。因为在这两个数组中,第一个数字,最后一个数字,中间数字都是1。

第一种情况下,中间数字位于后面的子数组,第二种情况,中间数字位于前面的子数组。

因此当两个指针指向的数字和中间数字相同的时候,我们无法确定中间数字1是属于前面的子数组(绿色表示)还是属于后面的子数组(紫色表示)。

也就无法移动指针来缩小查找的范围。

【代码】

/*********************************
*   日期:2015-01-31
*   作者:SJF0115
*   题目: 154.Find Minimum in Rotated Sorted Array II
*   网址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int findMin(vector<int> &num) {
        int left = 0,right = num.size() - 1;
        int mid = 0;
        // num[left] >= num[right] 确保旋转
        while(num[left] >= num[right]){
            // 分界点
            if(right - left == 1){
                mid = right;
                break;
            }//if
            mid = left + (right - left) / 2;
            // num[left] num[right] num[mid]三者相等
            // 无法确定中间元素是属于前面还是后面的递增子数组
            // 只能顺序查找
            if(num[left] == num[right] && num[left] == num[mid]){
                return MinOrder(num,left,right);
            }//if
            // 中间元素位于前面的递增子数组
            // 此时最小元素位于中间元素的后面
            if(num[mid] >= num[left]){
                left = mid;
            }//if
            // 中间元素位于后面的递增子数组
            // 此时最小元素位于中间元素的前面
            else{
                right = mid;
            }
        }//while
        return num[mid];
    }
private:
    // 顺序寻找最小值
    int MinOrder(vector<int> &num,int left,int right){
        int result = num[left];
        for(int i = left + 1;i < right;++i){
            if(num[i] < result){
                result = num[i];
            }//if
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    //vector<int> num = {0,1,2,3,4,5};
    vector<int> num = {1,0,1,1,1,1,1};
    int result = solution.findMin(num);
    // 输出
    cout<<result<<endl;
    return 0;
}



【代码二】

class Solution {
public:
    int findMin(vector<int> &num) {
        int left = 0,right = num.size() - 1;
        int mid = 0;
        while(left < right){
            mid = left + (right - left) / 2;
            if(num[mid] > num[right]){
                left = mid + 1;
            }//if
            else if(num[mid] < num[right]){
                right = mid;
            }//else
            // 不能确定最小元素在哪边
            else{
                --right;
            }
        }//while
        return num[left];
    }
};



目录
相关文章
|
6月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
16 0
|
6月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
19 0
Search in Rotated Sorted Array - 循环有序数组查找问题
Search in Rotated Sorted Array - 循环有序数组查找问题
51 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
58 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
12天前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
15 3
|
3月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
3月前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
4月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
30 3
|
5月前
|
Ruby

热门文章

最新文章