C++ Standard Binary Search


  • 0
    F
    class Solution {
    public:
        int guessNumber(int n) {
            int l = 1, r = n;
            while (l <= r)
            {
                int mid = l + (r - l)/2;
                switch (guess(mid)) 
                {
                   case 1: l = mid+1; break;
                   case -1: r = mid-1; break;
                   default: return mid;
                }
            }
            return *"It doesn't allow me to compile if I don't return something here";
        }
    };
    

  • 0
    A

    I just used while(1) to avoid the compiler error :)


Log in to reply
 

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