Just use binary search to find the first bad version. The code is as follows.
1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int version); 3 4 class Solution { 5 public: 6 int firstBadVersion(int n) { 7 int l = 1, r = n; 8 while (l < r) { 9 int m = l + (r - l) / 2; 10 if (isBadVersion(m)) r = m; 11 else l = m + 1; 12 } 13 return l; 14 } 15 };
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109342.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...