二分查找的刷题笔记

 leetcode题目

Easy档

704. Binary Search

35. Search Insert Position

2529. Maximum Count of Positive Integer and Negative Integer

278. First Bad Version

374. Guess Number Higher or Lower

3065. Minimum Operations to Exceed Threshold Value I

169. Majority Element

1351. Count Negative Numbers in a Sorted Matrix

Medium档

34. Find First and Last Position of Element in Sorted Array

658. Find K Closest Elements

229. Majority Element II

2780. Minimum Index of a Valid Split

915. Partition Array into Disjoint Intervals

模版

def binary_search_function(self, nums, target):
    #当报错的时候
    #就在开头把一些case特殊写出来
    if nums is None:
        return -1
    start, end = 0, len(nums)-1
    while start+1<end:
        mid = (start+end)//2
        if nums[mid]<target:
            start = mid
        elif nums[mid]==target:
            end = mid
        else:
            end = mid

    # 找first position of target
    if nums[start]==target:
        return start
    elif nums[end] == target:
        return end
    else:
        return -1       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值