File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Time: O(logn)
2
+ # Space: O(1)
3
+
4
+ # Given an array nums containing n + 1 integers where each integer is
5
+ # between 1 and n (inclusive), prove that at least one duplicate number
6
+ # must exist. Assume that there is only one duplicate number, find the duplicate one.
7
+ #
8
+ # Note:
9
+ # You must not modify the array (assume the array is read only).
10
+ # You must use only constant, O(1) extra space.
11
+ # Your runtime complexity should be less than O(n2).
12
+ # There is only one duplicate number in the array, but it could be repeated more than once.
13
+
14
+ # The guess API is already defined for you.
15
+ # @param num, your guess
16
+ # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
17
+ # def guess(num):
18
+
19
+ class Solution (object ):
20
+ def guessNumber (self , n ):
21
+ """
22
+ :type n: int
23
+ :rtype: int
24
+ """
25
+ left , right = 1 , n
26
+ while left <= right :
27
+ mid = left + (right - left ) / 2
28
+ if guess (mid ) <= 0 :
29
+ right = mid - 1
30
+ else :
31
+ left = mid + 1
32
+ return left
You can’t perform that action at this time.
0 commit comments