-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode704.java
More file actions
28 lines (24 loc) · 803 Bytes
/
LeetCode704.java
File metadata and controls
28 lines (24 loc) · 803 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
public class LeetCode704 {
public int search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = ((low + high) / 2); // Prevents overflow
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Target not found
}
public static void main(String[] args) {
LeetCode704 l = new LeetCode704();
int[] nums = {-1, 0, 3, 5, 9, 12};
int target = 9;
int result = l.search(nums, target);
System.out.println("Index of target " + target + ": " + result);
}
}