Skip to content

Commit 1198a1c

Browse files
authored
Create FirstBadVersion.java
1 parent fd4b0ea commit 1198a1c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

FirstBadVersion.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* The isBadVersion API is defined in the parent class VersionControl.
2+
boolean isBadVersion(int version); */
3+
4+
public class Solution extends VersionControl {
5+
public int firstBadVersion(int n) {
6+
7+
int left = 1, right = n;
8+
9+
while(left <= right) {
10+
int mid = left + (right - left) / 2;
11+
12+
if (!isBadVersion(mid)) {
13+
left = mid + 1;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
19+
return left;
20+
21+
}
22+
}

0 commit comments

Comments
 (0)