Skip to content

Instantly share code, notes, and snippets.

@stellaqx
Created October 12, 2021 05:12
Show Gist options
  • Save stellaqx/5dba75b4d02872ec9ef871814517410b to your computer and use it in GitHub Desktop.
Save stellaqx/5dba75b4d02872ec9ef871814517410b to your computer and use it in GitHub Desktop.
/**
* class SVNRepo {
* public:
* static bool isBadVersion(int k);
* }
* you can use SVNRepo::isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
class Solution {
public:
/**
* @param n: An integer
* @return: An integer which is the first bad version.
*/
int findFirstBadVersion(int n) {
int start = 1, end = n;
while(start + 1 < end) {
int mid = start + (end - start ) / 2;
if (SVNRepo::isBadVersion(mid)) {
end = mid;
} else {
start = mid;
}
}
if (SVNRepo::isBadVersion(start)) return start;
return end;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment