---
# System prepended metadata

title: Leetcode 278. First Bad Version
tags: [Leetcode(C++)]

---

# Leetcode 278. First Bad Version
###### tags: `Leetcode(C++)`

題目 : https://leetcode.com/problems/first-bad-version/ 。

想法 : 二分搜改編版。

時間複雜度 : O(logn)。

程式碼 :

```
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        unsigned int l = 1, r = n, mid;
        while(l != r){
            mid = (l + r) / 2;
            if(isBadVersion(mid) == false) l = mid + 1;
            else r = mid;
        }
        
        return l;
    }
};
```