---
# System prepended metadata

title: \#278 First Bad Version
tags: [leetcode, leetcode-easy]

---

# \#278 First Bad Version
## *有n版本, 給定一function isBadVersion可判斷版本是否為錯誤版, 回傳最小錯誤版本*
## Log
 - build 20210605 by syhuang

## xxx
 - xxx
```javascript=
```
## 初見
 - 二分法查找
```javascript=
var solution = function(isBadVersion) {
    /**
     * @param {integer} n Total versions
     * @return {integer} The first bad version
     */
    return function(n) {
        var left=0;
        var right=n;
        while(left<=right){
            var mid = left + Math.ceil((right-left)/2);
            if(isBadVersion(mid)) right = mid-1;
            else left = mid + 1;
        }
        return right + 1;
    };
};
```
## 備註
 - 二分法查找定式:
```javascript=
while (right - left > 1) {
    var mid = left + Math.ceil((right-left)/2);
    if (check(mid))
        left = mid;
    else
        right = mid;
}
```
## 參考
###### tags: `leetcode`, `leetcode-easy`