-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution11.java
More file actions
40 lines (37 loc) · 986 Bytes
/
Copy pathSolution11.java
File metadata and controls
40 lines (37 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package sword_offer;
//page 82 旋转数组的最小数字
public class Solution11 {
public static int min(int[] arr) {
int start = 0;
int end = arr.length - 1;
if (arr[start] < arr[end]) //旋转数组是其本身的情形
return arr[start];
while (start <= end) {
int mid = start + ((end - start) >> 1);
if (arr[mid] == arr[start] && arr[mid] == arr[end]) //考虑顺序查找情形
return min(arr, start, end);
if (arr[mid] >= arr[start])
start = mid;
else
end = mid;
if (start == end - 1)
break;
}
return arr[end];
}
//特殊情形下的顺序查找
private static int min(int[] arr, int start, int end) {
int result = arr[start];
for (int i = start + 1; i <= end; i++)
if (arr[i] < result)
result = arr[i];
return result;
}
//测试
public static void main(String[] args) {
int[] arr = { 4, 5, 1, 2, 3 };
int[] arr2 = { 1, 0 , 1, 1, 1 };
System.out.println(min(arr));
System.out.println(min(arr2));
}
}