0278.cpp (422B)
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 low = 1, high = n, last = -1; 8 while (low <= high) { 9 int mid = low + (high - low) / 2; 10 if (isBadVersion(mid)) 11 high = (last = mid) - 1; 12 else 13 low = mid + 1; 14 } 15 return last; 16 } 17 };