Skip to content

Commit 43c421d

Browse files
committed
Solve 278 with C++.
1 parent c3178f6 commit 43c421d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)