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 c3178f6 commit 43c421dCopy full SHA for 43c421d
solutions/278.First_Bad_Version/AC_binary_search_logn.cpp
@@ -0,0 +1,39 @@
1
+/*
2
+* Author: illuz <iilluzen[at]gmail.com>
3
+* File: AC_binary_search_logn.cpp
4
+* Create Date: 2015-09-24 09:33:56
5
+* Descripton:
6
+*/
7
+
8
+#include <bits/stdc++.h>
9
10
+using namespace std;
11
+const int N = 0;
12
13
+// Forward declaration of isBadVersion API.
14
+bool isBadVersion(int version) { return version >= 1; }
15
16
+class Solution {
17
+public:
18
+ int firstBadVersion(int n) {
19
+ int l = 1, r = n;
20
+ while (l < r) {
21
+ int mid = l + (r - l) / 2;
22
+ if (isBadVersion(mid)) {
23
+ r = mid;
24
+ } else {
25
+ l = mid + 1;
26
+ }
27
28
+ return l;
29
30
+};
31
32
+int main() {
33
+ int n;
34
+ Solution s;
35
+ cin >> n;
36
+ cout << s.firstBadVersion(n) << endl;
37
+ return 0;
38
+}
39
0 commit comments