Posted onEdited onInLeetCodeDisqus: Symbols count in article: 298Reading time ≈1 mins.
binary search O(logn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Forward declaration of isBadVersion API. boolisBadVersion(int version);
classSolution { public: intfirstBadVersion(int n){ int l = 1, r = n; while (l < r) { int m = l + (r - l) / 2; if (isBadVersion(m)) { r = m; } else { l = m + 1; } } return l; } };