-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosestNumber01.java
More file actions
43 lines (32 loc) · 887 Bytes
/
ClosestNumber01.java
File metadata and controls
43 lines (32 loc) · 887 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
41
42
43
package lintcode;
/**
* Created by KXC176 on 16/11/2017.
*/
public class ClosestNumber01 {
/**
* binary search
*
* 样例
* [1, 2, 3] target = 2, 返回 1.
* [1, 4, 6] target = 3, 返回 1.
* [1, 4, 6] target = 5, 返回 1 或者 2.
* [1, 3, 3, 4] target = 2, 返回 0 或者 1 或者 2.
*
* @param A
* @param target
* @return
*/
public int closestNumber(int[] A, int target){
if (A.length == 0) {
return -1;
}
int start = 0 ;
int end = A.length - 1; // length and index relation , have to minus 1
while (start+1 < end){
int mid = start + (end - start)/2; // why (end - start)/2? => take mid value
if (A[mid] == target){
return mid;
}
}
}
}