leetcode_154_Find Minimum in Rotated Sorted Array II

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢

154 Find Minimum in Rotated Sorted Array II 

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.

//方法一:正常的解法
class Solution {
public:
    int findMin(vector<int> &num) {
        if( num.size()==0 )
            return NULL;
        int length = num.size();
        int left = 0;
        int right = length-1;
        int mid = left;

        while( num[left] >= num[right])
        {
            if( right-left == 1)
            {
                mid = right;
                break;
            }

            mid = (left+right)/2;
            if( num[left]==num[right] && num[left]==num[mid] )
            {
                int result = num[left];
                for( int i=left+1 ; i<=right; i++)
                {
                    if( result > num[i] )
                        result = num[i];
                }
                return result;
            }

            if( num[left] <= num[mid] )
                left = mid;
            else if ( num[right] >= num[mid] )
                right = mid;
        }

        return num[mid];
    }
};
//方法二:网上方法,记住前面的方法即可
class Solution {
public:
    int findMin(vector<int> &num) {
        if( num.size()==0 )
            return NULL;
        int length = num.size();
        int left = 0;
        int right = length-1;

        while( left<right )
        {
            int mid = (left+right)/2;
            if( num[left] == num[right] )
                left++;
            else if ( num[left] < num[right] )
                break;
            else
            {
                if( num[left] <= num[mid])
                    left = mid+1;
                else if ( num[right] >= num[mid] )
                    right = mid;
            }
        }
        return num[left];
    }
};
时间: 2024-10-17 16:07:15

leetcode_154_Find Minimum in Rotated Sorted Array II的相关文章

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

Leetcode#154Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 25678 Total Submissions: 80978My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

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 be

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

【LeetCode】Find Minimum in Rotated Sorted Array II (2 solutions)

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

【LeetCode-面试算法经典-Java实现】【155-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

[154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Supp

Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

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. 解题思路: 参考Java for LeetCode 081 Search in Rotated Sorted Array II J

[LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 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 be

Leetcode[154]-Find Minimum in Rotated Sorted Array II

Link: https://leetcode.com/problems/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