278.第一个错误的版本
278.第一个错误的版本
题解
看到这一题,想起了第一次参加csp,csp的第一题看也看不懂,随便写个代码居然过了- -,这题也一样,令人费解
代码
package main /** * 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; */ func firstBadVersion(n int) int { left, right := 1, n for left+1 < right { mid := left + (right-left)/2 if !isBadVersion(mid) { left = mid } else { right = mid } } if isBadVersion(left) { return left } return right }