Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
55 lines (48 loc) · 1.48 KB

File metadata and controls

55 lines (48 loc) · 1.48 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
47
48
49
50
51
52
53
54
55
package Algorithms;
public class BinarySearch {
public static void main(String[] strs) {
BinarySearch bs = new BinarySearch();
int[] num = new int[]{0,1,2,3,4,5};
int target = 9;
System.out.print(bs.binarySearch(num, target));
//int[] num2 = new int[0];
//System.out.print(bs.binarySearch(num2, target));
}
public int binarySearch(int num[], int target) {
if (num == null || num.length == 0) {
return -1;
}
int left = 0;
int right = num.length - 1;
String test = "test";
String t2 = test.substring(0,0);
System.out.printf(test.substring(0,0));
// while (left <= right) {
// int mid = left + (right - left)/2;
// if (num[mid] > target) {
// right = mid - 1;
// } else if (num[mid] < target) {
// left = mid + 1;
// } else {
// return mid;
// }
// }
// [1, 2]
while (left + 1 < right) {
int mid = left + (right - left)/2;
if (num[mid] > target) {
right = mid;
} else if (num[mid] < target) {
left = mid;
} else {
return mid;
}
}
if (num[left] == target) {
return left;
} else if (num[right] == target) {
return right;
}
return -1;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.