---
# System prepended metadata

title: \#374 Guess Number Higher or Lower
tags: [leetcode, leetcode-easy]

---

# \#374 Guess Number Higher or Lower
## *簡單描述題目*
## Log
 - build 20210710 by syhuang

## 初見
 - 二分法查找
 - 注意邊界
```javascript=
var guessNumber = function(n) {
    if(guess(n)==0) return n;
    if(guess(1)==0) return 1;
    let left = 1;
    let right = n;
    let result;
    while(right-left>1){
        let mid = left + Math.ceil((right-left)/2);
        let check = guess(mid);
        if(check==-1) right = mid;
        else if(check==1) left = mid
        else {
            result = mid;
            break;
        }
    }
    return result;
};
```
## 備註
## 參考
###### tags: `leetcode`, `leetcode-easy`