Skip to content

Commit

Permalink
FirstBadVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Mar 6, 2018
1 parent f52fc0d commit 8bdd36b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/com/bzh/leetcode/sort_search/FirstBadVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bzh.leetcode.sort_search;

import org.junit.Assert;
import org.junit.Test;

public class FirstBadVersion {
boolean isBadVersion(int version) {
return version >= 2;
}

@Test
public void tst() {
Assert.assertEquals(2, firstBadVersion(100));
}

//https://www.zhihu.com/question/36132386
public int firstBadVersion(int n) {

int left = 1, right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
if (isBadVersion(mid)) right = mid-1;
else left = mid+1;
}
return left;
}

}

0 comments on commit 8bdd36b

Please sign in to comment.