[Python]二分查找算法实现

二分查找算法程序如下:

语言环境:Python 3.5

class Solution:
    def binarysearch(self, nums, target):
        """ :type nums:list :type target: int :rtype:int """
        left = 0
        right = len(nums)-1
        while left <= right:
            middle = (left+right)//2
            if nums[middle]==target:
                return middle
            elif nums[middle] < target:
                left = middle + 1
            else:
                right = middle - 1

        return -1
    原文作者:查找算法
    原文地址: https://blog.csdn.net/jiayk2016/article/details/79511606
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞