添加0704.二分查找Python版本

This commit is contained in:
Tiansheng Sui 2021-05-11 23:38:24 -07:00 committed by GitHub
parent cdc9b27c68
commit 1f609be3b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -151,6 +151,22 @@ Java
Python
```python3
class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
middle = (left + right) // 2
if nums[middle] < target:
left = middle + 1
elif nums[middle] > target:
right = middle - 1
else:
return middle
return -1
```
Go