LC.278. First Bad Version

https://leetcode.com/problems/first-bad-version/description/

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version.

You should minimize the number of calls to the API.

time: log(n)

space: o(1)

 1 public int firstBadVersion(int n) {
 2         int left = 1, right = n;
 3         while (left + 1 < right) {
 4             int mid = left + (right - left) / 2;
 5             if (isBadVersion(mid)) {
 6                 right = mid;
 7             } else if (!isBadVersion(mid)) {
 8                 left = mid;
 9             } else {
10                 right = mid;
11             }
12         }
13         //post processing: same as first occurance
14         if (isBadVersion(left)) {
15             return left;
16         }
17         if (isBadVersion(right)) {
18             return right;
19         }
20         return -1;
21     }

原文地址:https://www.cnblogs.com/davidnyc/p/8587022.html

时间: 2024-10-11 17:36:20

LC.278. First Bad Version的相关文章

LeetCode 278 First Bad Version

LeetCode  278 First Bad Version // Forward declaration of isBadVersion API. bool isBadVersion(int version); int firstBadVersion(int n) { int start=1, end=n; while(start < end) { int mid=start+(end-start)/2; if(isBadVersion(mid)) end=mid; else start=m

[Lintcode]74. First Bad Version/[Leetcode]278. First Bad Version

[Lintcode]74. First Bad Version [Leetcode]278. First Bad Version 本题难度: [Lintcode]Medium/[Leetcode] Topic: Binary Search Description 我的代码 for Leetcode(在Lintcode中因为isBadVersion调用太多次无法通过) # The isBadVersion API is already defined for you. # @param versi

Leetcode 278 First Bad Version 二分查找

题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int version); 3 4 class Solution { 5 public: 6 int firstBadVersion(int n) { 7 int l = 1, r = n , ans ; 8 while(l <= r){ 9 int m

LeetCode 278 First Bad Version(第一个坏版本)(二分法)(*)

翻译 你是一个产品经理,目前正在带领团队去开发一个新产品. 不幸的是,产品的上一个版本没有通过质量检测. 因为每个版本都是建立在前一个版本基础上开发的,所以坏版本之后的版本也都是坏的. 假设你有n个版本[1,2,...,n],并且你想找出造成后面所有版本都变坏的第一个坏版本. 给你一个API--bool isBadVersion(version),它能够确定一个版本是否是坏的. 实现一个函数去找出第一个坏版本. 你应该尽可能少地去调用这个API. 原文 You are a product man

278. First Bad Version

https://leetcode.com/problems/first-bad-version/#/description You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed

278. First Bad Version 折半查找,分治法

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve

278. First Bad Version(二分查找)

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve

LeetCode:First Bad Version - 第一个坏版本

1.题目名称 First Bad Version(第一个坏版本) 2.题目地址 https://leetcode.com/problems/first-bad-version/ 3.题目内容 英文: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c

[LeetCode] 165. Compare Version Numbers 比较版本数

Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte