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 based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

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.

Sol 1:

Binary search

class Solution(object):
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """

        if isBadVersion(1):
            return 1
        l = 1
        r = n
        while l < r - 1:
            mid = (l+r)/2
            if isBadVersion(mid):
                r = mid
            else:
                l = mid
        return r

Sol 2:

Recursion

class Solution(object):
    def rec(self,l,r):
        if(l>r):
            return 0
        else:
            mid=(l+r)/2
            if(isBadVersion(mid)):
                if(l==r):
                    return l
                else:
                    return self.rec(l,mid)
            else:
                return self.rec(mid+1,r)

    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        ans=self.rec(1,n)
        return ans

 

时间: 2024-11-05 04:53:13

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

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

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