-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
38 lines (31 loc) · 1011 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
38 lines (31 loc) · 1011 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
class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0)
return -1;
if (nums.length == 1 && nums[0] == target) {
return 0;
}
int mid = nums.length / 2;
int start = 0;
int end = nums.length - 1;
while ((end - start) >= 1) {
if (nums[mid] == target)
return mid;
if (target > nums[mid])
start = ++mid;
else
end = mid;
mid = start + (end - start) / 2;
}
if (mid == target)
return mid;
return -1;
}
}
/*
* Bierzemy indeksy start, end, mid
* jeżeli nums[mid] == target, zwracamy indeks
* jeżeli target > nums[mid], ustawiamy start na mid, ustawiamy mid na (end - start) / 2
* jeżeli target < nums[mid], ustawiamy end na mid, ustawiamy na (end - start) / 2
* Robimy powyższe w pętli while, dopóki
*/