Skip to content

Commit a4f3992

Browse files
author
王俊超
committed
commit
1 parent ecb33a6 commit a4f3992

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* <pre>
3+
* The isBadVersion API is defined in the parent class VersionControl.
4+
* boolean isBadVersion(int version);
5+
* </pre>
6+
*
7+
* @author: wangjunchao(王俊超)
8+
* @time: 2018-10-10 14:26
9+
**/
10+
public class Solution extends VersionControl {
11+
public int firstBadVersion(int n) {
12+
13+
if (!isBadVersion(n)) {
14+
return 0;
15+
}
16+
17+
int hi = n;
18+
int lo = 1;
19+
int mid;
20+
21+
while (hi >= lo) {
22+
mid = lo + (hi - lo) / 2;
23+
if (isBadVersion(mid)) {
24+
// 当前是1号版本,或者前一个是合法版本,那么当前版本就是第一个非法的版本
25+
if (mid == 1 || (mid > 1 && !isBadVersion(mid - 1))) {
26+
return mid;
27+
} else {
28+
// 非法的版本在[lo, mid-1]间
29+
hi = mid - 1;
30+
}
31+
} else {
32+
// 非法版本在[mid + 1, hi]间
33+
lo = mid + 1;
34+
}
35+
}
36+
37+
return 0;
38+
}
39+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 16:55
4+
**/
5+
public class Test {
6+
public static void main(String[] args) {
7+
Solution solution = new Solution();
8+
9+
solution.setBadVersion(new boolean[]{false, false, false, true, true});
10+
System.out.println(solution.firstBadVersion(5));
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-10-10 14:27
4+
**/
5+
public class VersionControl {
6+
7+
private boolean[] badVersion;
8+
9+
public boolean isBadVersion(int version){
10+
return badVersion[version - 1];
11+
}
12+
13+
public boolean[] getBadVersion() {
14+
return badVersion;
15+
}
16+
17+
public void setBadVersion(boolean[] badVersion) {
18+
this.badVersion = badVersion;
19+
}
20+
}

0 commit comments

Comments
 (0)