---
# System prepended metadata

title: 0278. First Bad Version
tags: [Leetcode, Easy, Binary Search]

---

# 0278. First Bad Version
###### tags: `Leetcode` `Easy` `Binary Search`
Link: https://leetcode.com/problems/first-bad-version/comments/
##  思路
其实就是很简单的二分法查找
但一定要记得**二分法查找的边界条件要想清楚**，以及```mid = left+(right-left)/2```以防止溢出（mid =(left+right)/2， 如果left+right超出int范围，会出现Time Exceed Error）
## Code
```java=
public int firstBadVersion(int n) {
        if(isBadVersion(1)==true) return 1;
        int left = 1;
        int right = n;
        while(left<right){
            int mid = left + (right - left) / 2;
            boolean flag = isBadVersion(mid);
            if(flag == true){
                right = mid;
            }
            else{
                left = mid+1;
            }
        }
        return left;
    }
