[leetcode] FindMinimuminRotatedSortedArray2

/**
*
* <pre>
* 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.
* </pre>
* */
    public class Solution {

        public int findMin(int[] num) {
            return _findMin(num, 0, num.length - 1);
        }

        private int _findMin(int[] num, int start, int end) {
            if (num[start] < num[end]) {
                return num[start];
            }

            if (start == end) {
                return num[end];
            }

            if (start + 1 == end) {
                return num[end];
            }

            int mid = (end + start) / 2;
            if (num[mid] < num[start]) {
                return _findMin(num, start, mid);
            } else if (num[mid] > num[start]) {
                return _findMin(num, mid, end);
            } else {
                return _findMin(num, start + 1, end);
            }
        }
    }

你可能感兴趣的:(LeetCode)