Skip to content

Commit 82926b4

Browse files
authored
Create LeetCode_278_FirstBadVersion.java
1 parent 4c674a0 commit 82926b4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
int left = 1;
7+
int right = n;
8+
9+
int answer = -1;
10+
11+
while (left <= right) {
12+
int mid = right + (left - right) / 2;
13+
14+
if (isBadVersion(mid)) {
15+
answer = mid;
16+
right = mid - 1;
17+
} else {
18+
left = mid + 1;
19+
}
20+
}
21+
22+
return answer;
23+
}
24+
}

0 commit comments

Comments
 (0)