---
title: "278. First Bad Version"
url: "https://laigary.com/interview/coding/278-first-bad-version"
type: "note"
section: "coding"
date: "2023-01-28"
updated: "2024-03-03"
---

# 278. First Bad Version

[278\. First Bad Version](https://leetcode.com/problems/first-bad-version/)

1.  如果中間值是錯誤的版本，代表最早的錯誤版本在左區間
2.  如果中間值是正確的版本，代表最早的錯誤版本在右區間

```python
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        left = 1
        right = n
        while left < right:
            mid = left + (right - left) // 2
            if isBadVersion(mid):
                right = mid
            else:
                left = mid + 1

        return left
```
