24 lines
566 B
Python
24 lines
566 B
Python
from typing import List
|
|
|
|
|
|
class Solution:
|
|
def search(self, nums: List[int], target: int) -> int:
|
|
left = 0
|
|
right = len(nums) - 1
|
|
while left <= right:
|
|
mid = (right + left) // 2
|
|
if target < nums[mid]:
|
|
right = mid - 1
|
|
elif target > nums[mid]:
|
|
left = mid + 1
|
|
else:
|
|
return mid
|
|
return -1
|
|
|
|
|
|
s = Solution()
|
|
# nums = [-1, 0, 2, 4, 6, 8]
|
|
# print(s.search(nums, 4))
|
|
# print(s.search(nums, 3))
|
|
nums = [-1, 0, 3, 5, 9, 12]
|
|
print(s.search(nums, 9))
|