-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
38 lines (36 loc) · 926 Bytes
/
Copy pathsolution.java
File metadata and controls
38 lines (36 loc) · 926 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
// 278. First Bad Version
// https://leetcode.com/problems/first-bad-version/
// Easy | Java | Accepted 2022-08-06
// Runtime 17 ms | Memory 38.9 MB
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int m) {
int first = 1;
int last = m;
while(first<last)
{
int mid = first+(last-first)/2;
if(isBadVersion(mid)==false)
{
first = mid + 1;
}
else
{
last = mid;
}
}
return first;
}
}
/*
int start = 1;
int end = m;
while (start < end) {
int mid = start + (end-start) / 2;
if (!isBadVersion(mid)) start = mid + 1;
else end = mid;
}
return start;
}
*/