/** * Forward declaration of guess API. * @param num your guess * @return -1 if num is higher than the picked number * 1 if num is lower than the picked number * otherwise return 0 * int guess(int num); */ class Solution { public: // int guess(int num) // { // if(num>) // } int guessNumber(int n) { int s=1, e=n; int mid=s+(e-s)/2; while(s<=e) { int num=guess(mid); if(num==0) return mid; if(num==-1) e=mid-1; else s=mid+1; mid=s+(e-s)/2; } return mid; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter