LeetCode 153. Find Minimum in Rotated Sorted Array

Posted dacc123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 153. Find Minimum in Rotated Sorted Array相关的知识,希望对你有一定的参考价值。

题目

二分

一不小心耗时超过100%的c++解决方案。哈哈哈

class Solution {
public:
    int findMin(vector<int>& nums) {
        
        int l=0;
        int r=nums.size()-1;
        
        while(l<r)
        {
            int mid = (l+r)/2;
            if(nums[mid]>=nums[l]&&nums[mid]<=nums[r])
            {
                if(nums[mid]==nums[r])
                    r=mid-1;
                else
                    r=mid;
                continue;
            }
            if(nums[mid]>=nums[l]&&nums[mid]>=nums[r])
            {
                if(nums[mid]==nums[l])
                    l=mid+1;
                else
                    l=mid;
                continue;
            }
            if(nums[mid]<=nums[l]&&nums[mid]>=nums[r])
            {
                 if(nums[mid]==nums[l])
                    l=mid+1;
                else
                    l=mid;
                continue;
            }
            if(nums[mid]<=nums[l]&&nums[mid]<=nums[r])
            {
                if(nums[mid]==nums[r])
                    r=mid-1;
                else
                    r=mid;
                continue;
            }

        }
        
        return nums[l];
        
    }
};

以上是关于LeetCode 153. Find Minimum in Rotated Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 153. Find Minimum in Rotated Sorted Array

LeetCode-153-Find Minimum in Rotated Sorted Array

LeetCode 153 Find Minimum in Rotated Sorted Array

153. Find Minimum in Rotated Sorted Array - LeetCode

**Leetcode 153. Find Minimum in Rotated Sorted Array

leetcode 153. Find Minimum in Rotated Sorted Array --------- java