Skip to content

Commit 0d49fb6

Browse files
authored
Add files via upload
1 parent df99dc7 commit 0d49fb6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

278. First Bad Version.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// The API isBadVersion is defined for you.
2+
// bool isBadVersion(int version);
3+
4+
class Solution {
5+
public:
6+
int firstBadVersion(int n) {
7+
int left = 1, right = n;
8+
9+
while (left < right)
10+
{ // Stop when left == right
11+
int mid = left + (right - left) / 2;
12+
13+
if (isBadVersion(mid))
14+
{
15+
right = mid; // First bad version is at mid or before
16+
}
17+
else
18+
{
19+
left = mid + 1; // Search in the right half
20+
}
21+
}
22+
23+
return left; // Left now points to the first bad version
24+
}
25+
};

0 commit comments

Comments
 (0)