first_bad_version

  • 2022-12-14
  • 浏览 (490)

first_bad_version.cpp 源码

// The API isBadVersion is defined for you.
 bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        int low = 1, high = n;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (isBadVersion(mid)) {
                if (mid == low || !isBadVersion(mid-1)) {
                    return mid;
                }
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
};

你可能感兴趣的文章

find_min_rotated_array

first_last_in_array

search_rotate_array

0  赞