leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0278.cpp (422B)
0 // The API isBadVersion is defined for you. 1 // bool isBadVersion(int version); 2 3 class Solution { 4 public: 5 int firstBadVersion(int n) { 6 int low = 1, high = n, last = -1; 7 while (low <= high) { 8 int mid = low + (high - low) / 2; 9 if (isBadVersion(mid)) 10 high = (last = mid) - 1; 11 else 12 low = mid + 1; 13 } 14 return last; 15 } 16 };