C++ simple recursive 0ms, 3 main lines


  • 0
    class Solution {
    public:
        int guessNumber(int n) {
            return helper(1, n);
        }
        
        int helper(int l, int r) {
            int m = l + (r - l) / 2;
            return guess(m) == 0 ? m : (guess(m) == -1 ? helper(l, m - 1) : helper(m + 1, r));
        }
    };
    

    Or you can use long type to get rid of larger input case.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.