first_bad_version

  • 2022-12-14
  • 浏览 (538)

first_bad_version.go 源码

package leetcode

// 第一个错误的版本

/**
 * Forward declaration of isBadVersion API.
 * @param   version   your guess about first bad version
 * @return 	 	      true if current version is bad
 *			          false if current version is good
 */

// 由系统提供实现
func isBadVersion(version int) bool {
	return false
}

func firstBadVersion(n int) int {
	low, high := 1, n
	for low <= high {
		mid := low + (high-low)/2
		if isBadVersion(mid) {
			if mid == low || !isBadVersion(mid-1) {
				return mid
			}
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	return high
}

你可能感兴趣的文章

first_and_last_in_sorted_array

min_in_rotated_sorted_array

minimum_genetic_mutation

0  赞