/* The isBadVersion API is defined in the parent class VersionControl. bool IsBadVersion(int version); */ public class Solution : VersionControl { public int FirstBadVersion(int n) { var start = 1; var end = n; while (start < end) { var mid = start + (end - start) / 2; if (!IsBadVersion(mid)) { start = mid + 1; } else { end = mid; } } return start; } }
https://leetcode.com/problems/first-bad-version/#/description
1 /* The isBadVersion API is defined in the parent class VersionControl. 2 boolean isBadVersion(int version); */ 3 4 public class Solution extends VersionControl { 5 public int firstBadVersion(int n) { 6 int low = 0; 7 int high = n; 8 while(low < high){ 9 int mid = low + (high - low) / 2; 10 boolean result = isBadVersion(mid); 11 if(result){ 12 high = mid; 13 }else{ 14 low = mid + 1; 15 } 16 } 17 return low; 18 } 19 }