`
zhang_xzhi_xjtu
  • 浏览: 536536 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[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);
            }
        }
    }
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics