landscapemi的博客

# leetcode :Binary Search： Guess Number Higher or Lower(374)

We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I’ll tell you whether the number is higher or lower.

You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
n = 10, I pick 6.

Return 6.

// Forward declaration of guess API.
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);

class Solution {
public:
int guessNumber(int n) {
if (guess(n) == 0) return n;
int left = 1, right = n;
while (left < right) {
int mid = left + (right - left) / 2, t = guess(mid);
if (t == 0) return mid;
else if (t == 1) left = mid;
else right = mid;
}
return left;
}
};

#### [leetcode] 374. Guess Number Higher or Lower 解题报告

2016-07-16 03:13:27

#### LeetCode 374. Guess Number Higher or Lower

2016-07-13 16:06:03

#### <LeetCode OJ> 374. Guess Number Higher or Lower

2016-07-14 21:16:35

#### Leetcode 374. Guess Number Higher or Lower 桌游猜数字 解题报告

2016-07-13 15:35:15

#### leetcode 375. Guess Number Higher or Lower II 解题小结

2016-07-19 02:08:07

#### [leetcode] 375. Guess Number Higher or Lower II 解题报告

2016-07-22 08:03:14

#### 关于LeetCode中Guess Number Higher or Lower一题的理解

2016-09-08 13:52:13

#### leetcode(375):Guess Number Higher or Lower II

2016-08-11 17:43:52

#### [leetcode-375]Guess Number Higher or Lower II(java)

2016-07-17 16:48:24

#### 374. Guess Number Higher or Lower [easy] (Python)

2016-07-26 19:49:27