We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent df99dc7 commit 0d49fb6Copy full SHA for 0d49fb6
278. First Bad Version.cpp
@@ -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