标签:style blog http color io os ar 使用 java
Find Minimum in Rotated Sorted Array
Question Solution
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.
You may assume no duplicate exists in the array.
解答1:
这个题目trick的地方在于,它的旋转pivot次数未知。所以有可能它转了一圈又转回了原处
所以我们代码要处理2种情况:
我们还是用二分法来做。比起全部扫一次,二分法可以做到LogN的复杂度。
1. Sorted
这种情况简单,先计算出mid的值,如果它处在左右的中间,证明这个数列是正常的sorted,直接返回左边。
1 // Solution 1: 2 public int findMin1(int[] num) { 3 if (num == null || num.length == 0) { 4 return 0; 5 } 6 7 if (num.length == 1) { 8 return num[0]; 9 } 10 11 12 // 至少有2个元素,才有讨论的价值 13 int l = 0; 14 int r = num.length - 1; 15 16 while (l < r) { 17 int mid = l + (r - l)/2; 18 // Means that there is no rotate. 19 if (num[mid] >= num[l] && num[mid] <= num[r]) { 20 return num[0]; 21 } 22 23 // rotate > 0的情况 24 if (l == r - 1) { 25 // 当只余下2个元素的时候,这里是断点,右边的是小值 26 return num[r]; 27 } 28 29 if (num[mid] >= num[l]) { 30 // The left side is sorted. Discard left. 31 l = mid; 32 } else { 33 // The right side is sorted. 34 r = mid; 35 } 36 } 37 38 return 0; 39 }
1 // solution 2: 2 public int findMin(int[] A) { 3 if (A == null || A.length == 0) { 4 return 0; 5 } 6 7 if (A.length == 1) { 8 return A[0]; 9 } else if (A.length == 2) { 10 return Math.min(A[0], A[1]); 11 } 12 13 // 至少有3个元素,才有讨论的价值 14 int l = 0; 15 int r = A.length - 1; 16 17 while (l < r - 1) { 18 int m = l + (r - l) / 2; 19 20 // means that there is no rotate. 21 if (A[m] < A[r] && A[m] > A[l]) { 22 return A[0]; 23 } 24 25 // left side is sorted. 26 if (A[m] > A[l]) { 27 l = m; 28 } else { 29 r = m; 30 } 31 } 32 33 return A[r]; 34 }
1 while (l < r - 1) { 2 int m = l + (r - l) / 2; 3 4 // means that there is no rotate. 5 ... 这里添加各种退出条件,比如找到了目标值等 8 9 // left side is sorted. 10 if (A[m] > A[l]) { 11 l = m; 12 } else { 13 r = m; 14 } 15 }
GitHub:
GitHub代码链接LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
标签:style blog http color io os ar 使用 java
原文地址:http://www.cnblogs.com/yuzhangcmu/p/4049030.html