-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEleven.java
More file actions
112 lines (102 loc) · 3.12 KB
/
Copy pathEleven.java
File metadata and controls
112 lines (102 loc) · 3.12 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package offer.problem11;
/**
* Created with IntelliJ IDEA
*
* @Author yuanhaoyue swithaoy@gmail.com
* @Description p82 11. 旋转数组的最小数字
* @Date 2018-11-26
* @Time 18:01
*/
public class Eleven {
/**
* 针对递增数组
*/
public int minNumberInRotateArray1(int[] array) {
int middle, low = 0, high = array.length - 1;
while (low < high) {
middle = (high + low) / 2;
if (array[middle] < array[low]) {
high = middle;
} else if (array[middle] > array[high]) {
low = middle + 1;
}
}
return array[high];
}
/**
* 针对非递减数组
*/
public int minNumberInRotateArray2(int[] array) {
int middle, low = 0, high = array.length - 1;
while (low < high) {
middle = (high + low) / 2;
if (array[middle] > array[high]) {
low = middle + 1;
//处理相等的情况
} else if (array[middle] == array[high]) {
high = high - 1;
} else if (array[middle] < array[high]) {
high = middle;
}
}
return array[low];
}
/**
* 非递减数组,方案3:
*
* @param number 数组
* @return 数组中最小的数
*/
private static int min(int[] number) {
if (number == null || number.length <= 0) {
throw new RuntimeException("");
}
// 第一个位置
int index1 = 0;
// 最后一个位置
int index2 = number.length - 1;
// 设置初始值
int indexMin = 0;
while (number[index1] >= number[index2]) {
if (index2 - index1 == 1) {
// 两个索引刚好相连
indexMin = index2;
break;
}
// 取中位数
indexMin = (index1 + index2) / 2;
// 如果 index1、index2、indexMin 三个索引指向数字
if (number[index1] == number[index2] && number[index1] == number[indexMin]) {
return minInorder(number, index1, index2);
}
if (number[index1] <= number[indexMin]) {
index1 = indexMin;
} else if (number[index2] >= number[indexMin]) {
index2 = indexMin;
}
}
return number[indexMin];
}
/**
* 通过单层遍历找出给定数组中最小的数
*
* @param number 给定的数组
* @param index1 搜索起始索引
* @param index2 搜索结束索引
* @return 数组中最小的数
*/
private static int minInorder(int[] number, int index1, int index2) {
int result = number[index1];
for (int i = index1 + 1; i <= index2; i++) {
if (result > number[i]) {
result = number[i];
}
}
return result;
}
public static void main(String[] args) {
// int[] array = {1,1,1,0,1,1};
int[] array = {3, 4, 5, 6, 7, 1, 2};
System.out.println(new Eleven().minNumberInRotateArray1(array));
}
}