704. Binary Search
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 59% Topics: Array, Binary Search
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(logn)
# Space: O(1)
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left, right = 0, len(nums)-1
while left <= right:
mid = left + (right-left)//2
if nums[mid] > target:
right = mid-1
elif nums[mid] < target:
left = mid+1
else:
return mid
return -1
Solution from kamyu104/LeetCode-Solutions · MIT