-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc_374.java
More file actions
57 lines (43 loc) · 1.01 KB
/
Lc_374.java
File metadata and controls
57 lines (43 loc) · 1.01 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
54
55
//Lc-374. Guess Number Higher or Lower
public class Lc_374 {
public static void main(String[] args) {
int n = 1000;
System.out.println(guessNumber(n));
}
static int guessNumber(int n){
int start = 1, end = n, mid;
int ans;
while(start <= end){
mid = start+(end-start)/2;
ans = guess(mid);
if(ans == 0){
return mid;
}
else if(ans == 1){
start = mid + 1;
}
else if(ans == -1){
end = mid - 1;
}
else if(ans == 404){
break;
}
}
return -1;
}
static int guess(int mid){
int pick = 2; // this is the pick, the secret number
if(pick == mid){
return 0;
}
else if(pick < mid){
return -1;
}
else if(pick > mid){
return 1;
}
else{
return -1;
}
}
}