-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path374.py
42 lines (34 loc) · 881 Bytes
/
374.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num):
def guess(num):
if 6 > num:
return 1
elif 6 < num:
return -1
else:
return 0
class Solution(object):
def guessNumber(self, n):
"""
:type n: int
:rtype: int
"""
start = 1
end = n
while start <= end:
mid = (start+end)/2
result = guess(mid)
if result == 1:
start = mid+1
elif result == -1:
end = mid-1
else:
return mid
return start
if __name__ == "__main__":
print Solution().guessNumber(10)