forked from cherryljr/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess Number Game.java
More file actions
53 lines (44 loc) · 1.33 KB
/
Guess Number Game.java
File metadata and controls
53 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
该题是典型的 二分法 的题目。
与 Search Insert Position 几乎相同。根据该思路编写程序即可。
/*
Description
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):
Example
n = 10, I pick 4 (but you don't know)
Return 4. Correct !
Tags
Binary Search Google
*/
/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */
public class Solution extends GuessGame {
/**
* @param n an integer
* @return the number you guess
*/
public int guessNumber(int n) {
int left = 1;
int right = n;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
int res = guess(mid);
if (res == 0) {
return mid;
}
else if (res == -1) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (guess(left) == 0) {
return left;
}
return right;
}
}