374. Guess Number Higher or Lower

Posted 高数考了59

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了374. Guess Number Higher or Lower相关的知识,希望对你有一定的参考价值。

 1 // Forward declaration of guess API.
 2 // @param num, your guess
 3 // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
 4 int guess(int num);
 5 
 6 class Solution 
 7 {
 8 public:
 9     int guessNumber(int n) 
10     {
11         int left=1,right=n,mid=0;
12         while(left<right)
13         {
14             mid=left+(right-left)/2;
15             if(guess(mid)==1)
16                 left=mid+1;
17             else if(guess(mid)==-1)
18                 right=mid;
19             else
20                 return mid;
21         }
22         return left;
23     }
24 };

二分,问题不大

以上是关于374. Guess Number Higher or Lower的主要内容,如果未能解决你的问题,请参考以下文章

374. Guess Number Higher or Lower

374. Guess Number Higher or Lower

374. Guess Number Higher or Lower

374. Guess Number Higher or Lower

374. Guess Number Higher or Lower

374. Guess Number Higher or Lower