题解 | #二分查找-I#

二分查找-I

https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b

    def search(self , nums: List[int], target: int) -> int:
        # write code here
        if len(nums) > 0:
            low = 0
            high = len(nums) - 1
            while low <= high:
                mid = (low + high) // 2
                if nums[mid] == target:
                    return mid
                elif nums[mid] < target:
                    low = mid + 1
                else:
                    high = mid - 1
        return -1

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务