Skip to content

Commit 7977007

Browse files
committed
Create 0374-guess-number-higher-or-lower.kt
1 parent 444c9d0 commit 7977007

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* The API guess is defined in the parent class.
3+
* @param num your guess
4+
* @return -1 if num is higher than the picked number
5+
* 1 if num is lower than the picked number
6+
* otherwise return 0
7+
* fun guess(num:Int):Int {}
8+
*/
9+
10+
class Solution:GuessGame() {
11+
override fun guessNumber(n:Int):Int {
12+
var l = 1
13+
var r = n
14+
while ( true ) {
15+
val mid = l + (r - l) / 2
16+
val res = guess(mid)
17+
if(res == 0) return mid
18+
if(res > 0) l = mid + 1
19+
if(res < 0) r = mid - 1
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)