leetcode 374. Guess Size (Python)

We're playing a guessing game. Rules of the game are as follows:
I n select a number from 1 to. I guess you need to choose which number.
Every time you guess wrong, I'll tell you this figure is big or small.
You call a pre-defined interfaces guess (int num), it will return three possible outcomes (-1, 1 or 0):

-1: I have a relatively small number
1: I figure larger
0: Congratulations! you guessed right!
Example:

Input: n = 10, pick = 6
Output: 6

class Solution ( Object ): 
    DEF the guessNumber (Self, n-): 
        "" "
         : n-type: int 
        : rtype: int 
        " ""
         A, B = 0 , n- 
        NUM = (A + B) // 2 
        the while GUESS (NUM !) = 0 :
             IF gUESS (NUM) == . 1 : # guess is too small 
                a = NUM + . 1 
            IF gUESS (NUM) == - . 1 : # guess is too large 
                B = num- . 1 
            NUM = (a + B) // 2 
        return NUM

 

Guess you like

Origin www.cnblogs.com/xiaotongtt/p/11324560.html