Leetcode 374. Guess number Higher or Lower

Source: Internet
Author: User

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

I pick a number from 1 to n. You had 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!

Basic Binary Search Problem:
1 classSolution (Object):2 def guessnumber (self, n):3         """4: Type N:int5: Rtype:int6         """7Low, High =1, N8         9          whileLow <=High :TenMid = (low + high)//2 OneAns =Guess (mid) A             ifAns = =1: -Low = mid +1 -elif ans = =-1: theHigh = mid-1 -             Else: -                 returnMid

Leetcode 374. Guess number Higher or Lower

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.