-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (29 loc) · 846 Bytes
/
Solution.java
File metadata and controls
33 lines (29 loc) · 846 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
package org.example.problems.binary_search;
import org.example.problems.SolutionInterface;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Binary Search";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/binary-search/";
}
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
int middleValue = nums[middle];
if (middleValue == target) {
return middle;
}
if (middleValue > target) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return -1;
}
}