题目描述
解法
思路:二分查找
注意:当第一个 isBadVersion(mid)的结果为true时,得到第一个错误的版本
// The API isBadVersion is defined for you. // bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int left = 1, right = n; while(left < right){ int mid = left + (right - left) /2; if(isBadVersion(mid)){ right = mid; // 答案在区间 [left, mid] 中 }else{ left = mid + 1; // 答案在区间 [mid+1, right] 中 } }
// 此时有 left == righ,即为答案 return left; } };
标签:right,int,isBadVersion,mid,版本,278,LeetCode,left From: https://www.cnblogs.com/zc-030/p/17270164.html