Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def search(self, nums: List[int], target: int) -> int:
- # Initialise our bounds to start and end of nums array
- left_index = 0
- right_index = len(nums) - 1
- # Loop while we have not yet searched all numbers
- while left_index <= right_index:
- # Search the middle number of our range
- mid_index = (left_index + right_index) // 2
- mid_num = nums[mid_index]
- # If target found, return mid_index
- if mid_num == target:
- return mid_index
- # If target is greater than mid number
- if mid_num < target:
- left_index = mid_index + 1
- # If target is less than mid number
- else:
- right_index = mid_index - 1
- # If target not found, return -1
- return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement