码迷,mamicode.com
首页 > 其他好文 > 详细

374. 猜数字大小

时间:2020-04-22 00:08:18      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:ram   code   other   http   param   src   inf   obj   div   

技术图片

 

 

 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. 猜数字大小

标签:ram   code   other   http   param   src   inf   obj   div   

原文地址:https://www.cnblogs.com/panweiwei/p/12748605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!