-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (29 loc) · 869 Bytes
/
Solution.java
File metadata and controls
46 lines (29 loc) · 869 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
44
45
46
package leetcode.binarySearch;
public class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int high = n-1;
int low = 0;
while(high >= low){
int mid = low + ( high - low ) / 2;
int number = nums[mid];
if(number == target){
return mid;
}
if(number < target){
low = mid+1;
} else {
high = mid-1;
}
}
return -1;
}
public static void main(String[] args) {
Solution s = new Solution();
int[] nums1 = {-1,0,3,5,9,12};
double resultExpected = 4;
double result = s.search(nums1, 13);
System.out.println("RESULT EXPECTED: " + resultExpected);
System.out.println("RESULT: " + result );
}
}