Merge pull request #28 from tianshengsui/master

添加0704.二分查找Python版本
This commit is contained in:
Carl Sun 2021-05-12 18:15:16 +08:00 committed by GitHub
commit fbe43be1f4
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