输出: 4
解释: 9 出现在 nums 中并且下标为 4
```python class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 # 位置1
while left <= right: # 位置2
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1 # 位置3
else:
return mid
return -1 ```
class Solution:
def search(self, nums: List[int], target: int) -> int:
left,right =0, len(nums) # 位置1
while left < right: # 位置2
mid = (left + right) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid # 位置3
else:
return mid
return -1