clueless coding // TODO: be smarter

LeetCode Guess Number Higher or Lower: CPP Solution



TL;DR Code Solution


int guessNumber(int n) {
    int left = 1;
    int right = n;

    while (left < right) {
        int middle = (right - left) / 2 + left;
        int result = guess(middle);
        if (result == 0) {
            return middle;
        }
        else if (result == 1) {
            left = middle + 1;
        }
        else {
            right = middle - 1;
        }
    }   
    return left;
}