-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT14.java
More file actions
22 lines (22 loc) · 614 Bytes
/
T14.java
File metadata and controls
22 lines (22 loc) · 614 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class T14 {
public int binarySearch(int[] nums, int target) {
//write your code here
int start = 0;
int end = nums.length-1;
int middle = 0;
while (start < end) { //不需要等号
middle = (end + start)/2;
if (nums[middle] < target) {
start = middle + 1;
} else if (nums[middle] > target) {
end = middle - 1;
} else {
end = middle;
}
}
if (nums[end] == target) {
return end;
}
return -1;
}
}