-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0278.cpp
More file actions
22 lines (20 loc) · 697 Bytes
/
0278.cpp
File metadata and controls
22 lines (20 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
private:
int findBadVersion(unsigned int left, unsigned int right) {
unsigned int average = (left + right) / 2;
if (isBadVersion(average)) return average;
return findBadVersion(average + 1, right);
}
public:
int firstBadVersion(int n) {
int previousBadVersion = 0;
int currentBadVersion = findBadVersion(1, n);
while (previousBadVersion != currentBadVersion) {
previousBadVersion = currentBadVersion;
currentBadVersion = findBadVersion(1, previousBadVersion);
}
return currentBadVersion;
}
};