Skip to content

Commit b4862ff

Browse files
author
Li Li
committed
add code of 278
1 parent a45d17e commit b4862ff

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)