374. 猜数字大小
Posted yuhong1103
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了374. 猜数字大小相关的知识,希望对你有一定的参考价值。
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. 猜数字大小的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)