leetcode 347guess number higher or lower 猜数字大小

// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);

class Solution {
public:
    int guessNumber(int n) {
        int left=1,right=n;
        while(left<=right){
            int mid=left+(right-left)/2;  
            int g=guess(mid);
            if(g==0)
                return mid;
            if(g<0)
                right=mid-1;
            else
                left=mid+1;
        }
        return 0;
    }
};

猜你喜欢

转载自www.cnblogs.com/joelwang/p/10703490.html