Advertisement
sweet1cris

Untitled

Jan 9th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. /**
  2.  * public class GitRepo {
  3.  *     public static boolean isBadVersion(int k);
  4.  * }
  5.  * you can use GitRepo.isBadVersion(k) to judge whether
  6.  * the kth code version is bad or not.
  7. */
  8. class Solution {
  9.     /**
  10.      * @param n: An integers.
  11.      * @return: An integer which is the first bad version.
  12.      */
  13.     public int findFirstBadVersion(int n) {
  14.         int start = 1, end = n;
  15.         while (start + 1 < end) {
  16.             int mid = start + (end - start) / 2;
  17.             if (SVNRepo.isBadVersion(mid)) {
  18.                 end = mid;
  19.             } else {
  20.                 start = mid;
  21.             }
  22.         }
  23.            
  24.         if (SVNRepo.isBadVersion(start)) {
  25.             return start;
  26.         }
  27.         return end;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement