码迷,mamicode.com
首页 > 其他好文 > 详细

374. 猜数字大小

时间:2020-04-26 21:22:03      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:param   forward   数字   turn   end   ESS   span   begin   color   

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

 

374. 猜数字大小

标签:param   forward   数字   turn   end   ESS   span   begin   color   

原文地址:https://www.cnblogs.com/yuhong1103/p/12781749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!