leetcode 278. First Bad Version
int firstBadVersion(int n) { // long start = 1; // long end = n; int start = 1; int end = n; int last = -1; while (start <= end) { // int mid = (start + end) / 2; // if use this way, var 'start' and 'end' must be long type. int mid = start + (end - start) / 2; if (isBadVersion(mid)) { last = mid; end = mid - 1; } else start = mid + 1; } if (last != -1) return last; return n; }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】