374. 猜数字大小
Posted panweiwei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了374. 猜数字大小相关的知识,希望对你有一定的参考价值。
1 # The guess API is already defined for you.
2 # @param num, your guess
3 # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
4 # def guess(num):
5
6 class Solution(object):
7 def guessNumber(self, n):
8 """
9 :type n: int
10 :rtype: int
11 """
12 i, j = 1, n
13 while i < j:
14 mid = i + int((j - i) / 2)
15 if guess(mid) == -1:
16 j = mid
17 elif guess(mid) == 1:
18 i = mid + 1
19 elif guess(mid) == 0:
20 return mid
21 return i
22
23
24 if __name__ == ‘__main__‘:
25 solution = Solution()
26 print(solution.guessNumber(10))
以上是关于374. 猜数字大小的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)