-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0074. First Bad Version.java
More file actions
32 lines (32 loc) · 982 Bytes
/
0074. First Bad Version.java
File metadata and controls
32 lines (32 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* public class GitRepo {
* public static boolean isBadVersion(int k);
* }
* you can use GitRepo.isBadVersion(k) to judge whether
* the kth code version is bad or not.
*/
public class Solution {
/**
* @param n: An integer
* @return: An integer which is the first bad version.
*/
public int findFirstBadVersion(int n) {
int low = 1;
int high = n;
int firstBad = Integer.MAX_VALUE, lastGood = Integer.MIN_VALUE;
while (low <= high) {
int mid = (high - low) / 2 + low; //in case n=2147483647
if (SVNRepo.isBadVersion(mid)) {
firstBad = Math.min(mid, firstBad);
high = mid - 1;
} else { //found false
lastGood = Math.max(mid, lastGood);
low = mid + 1;
}
if (lastGood == firstBad - 1) {
return firstBad;
}
}
return 1; //what if result is 1?
}
}