[LeetCode]#374. Guess Number Higher or Lower

Fatboy Slim
1 min readMar 25, 2020

--

Environment : Python 3.7

Key technique: Binary search

Example :

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

Solution:

class Solution:
def guessNumber(self, n: int) -> int:
low=1
high=n
while low <= high:
mid = (low+high) // 2
if guess(mid) == 1:
low = mid + 1
else:
high = mid - 1
return low

Reference:

https://leetcode.com/problems/guess-number-higher-or-lower/solution/

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet