[力扣] 算法 374 (C#)

374. 猜数字大小

public class Solution : GuessGame {
    public int GuessNumber(int right) {
        var left = 1;
        while (left < right)
        {
            int mid = (int)(((long)left + right) / 2);
            var ret = guess(mid);
            if (ret == 0) return mid;
            if (ret < 0) right = mid - 1;
            else left = mid + 1;
        }
        return left;
    }
}

发表评论