// The API isBadVersion is defined for you. // bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int s=0, e=n-1; int mid=s+(e-s)/2; int ans=-1; while(s<=e) { if(isBadVersion(mid)) { ans=mid; e=mid-1; } else s=mid+1; mid=s+(e-s)/2; } return mid; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter