// 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 i=1,j=n;
    while(i<j){
        int mid=i+(j-i)/2;
        if(guess(mid)==0){
            return mid;
        }else if(guess(mid)==1){
            i=mid+1;
        }else{
            j=mid;
        }        
    }
    return i;
    }
};

https://leetcode.com/problems/guess-number-higher-or-lower/#/description

posted on 2017-04-22 16:35  Sempron2800+  阅读(82)  评论(0编辑  收藏  举报