Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 378 Bytes

374.guess-number-higher-or-lower.md

File metadata and controls

19 lines (17 loc) · 378 Bytes

二分查找

模板题

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int l = 1, r = n;
        while (l < r){
            int mid = l + (r-l)/2;
            int re = guess(mid);
            if (re == 0) return mid;
            else if (re == -1) r = mid;
            else l = mid + 1;
        }
        return l;
    }
}