forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMin.java
More file actions
78 lines (63 loc) · 1.87 KB
/
FindMin.java
File metadata and controls
78 lines (63 loc) · 1.87 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Algorithms.binarySearch;
public class FindMin {
// Solution 1:
public int findMin1(int[] num) {
if (num == null || num.length == 0) {
return 0;
}
if (num.length == 1) {
return num[0];
}
// 至少有2个元素,才有讨论的价值
int l = 0;
int r = num.length - 1;
while (l < r) {
int mid = l + (r - l)/2;
// Means that there is no rotate.
if (num[mid] >= num[l] && num[mid] <= num[r]) {
return num[0];
}
// rotate > 0的情况
if (l == r - 1) {
// 当只余下2个元素的时候,这里是断点,右边的是小值
return num[r];
}
if (num[mid] >= num[l]) {
// The left side is sorted. Discard left.
l = mid;
} else {
// The right side is sorted.
r = mid;
}
}
return 0;
}
// solution 2:
public int findMin(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
if (A.length == 1) {
return A[0];
} else if (A.length == 2) {
return Math.min(A[0], A[1]);
}
// 至少有3个元素,才有讨论的价值
int l = 0;
int r = A.length - 1;
while (l < r - 1) {
int m = l + (r - l) / 2;
// means that there is no rotate.
if (A[r] > A[l]) {
return A[0];
}
// left side is sorted.
if (A[m] > A[l]) {
l = m;
} else {
r = m;
}
}
return A[r];
}
}