博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二分法
阅读量:5831 次
发布时间:2019-06-18

本文共 2729 字,大约阅读时间需要 9 分钟。

二分法查找

li = list(range(100000))#  二分法查找def bin_search(li, num):    high = len(li)-1    low = 0    while high >= low:        mid = (high + low) // 2        if li[mid] == num:            return mid        elif li[mid] > num:            high = mid -1        else:            low = mid + 1    return None# 尾递归二分法查找def bin_search2(li, num, low, high):    if low <= high:        mid = (low + high) // 2        if li[mid] == num:            return mid        elif li[mid] >= num:            bin_search2(li, num, low, mid-1)        else:            bin_search2(li, num, low+1, high)    else:        return

  

二分法示例

# 1.二分法查找相同数# Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.# Your algorithm's runtime complexity must be in the order of O(log n).# If the target is not found in the array, return [-1, -1].# For example,# Given [5, 7, 7, 8, 8, 10] and target value 8,# return [3, 4].def bin_search(li, target):    # 先利用二分法,匹配到mid。    # 开两个for循环,依次从mid往左和往右查找,直到第一个不等于TARGET的时候返回    low = 0    high = len(li) - 1    while low <= high:        mid = (low + high) // 2        if li[mid] == target:            a = mid            b = mid            while li[a] == target and a >= 0:  # and 条件在最开始                a -= 1            while li[b] == target and b < len(li) - 1:                b += 1            return (a + 1, b)        elif li[mid] > target:            high = mid - 1        else:            low = mid + 1    return Noneprint(bin_search([1, 1, 3, 5, 6, 7, 8, 9, 10, 10], 10))# 2.# Given an array of integers,# return indices of the two numbers such that they add up to a specific target.# You may assume that each input would have exactly one solution,# and you may not use the same element twice.# Example# Given nums = [2, 7, 11, 15], target = 9,# Because nums[0] + nums[1] = 2 + 7 = 9,# return [0, 1].nums = [2, 7, 11, 15]def twoSum(nums, target):    '''    时间复杂度O(N**2), 两层循环,依次匹配    :param nums:     :param target:     :return:     '''    for i in range(len(nums)):        for j in range(i + 1, len(nums)):            if nums[i] + nums[j] == target:                return i, j    return None# print(twoSum(nums, 18))def tow_sum_2(nums, target):# TODO:fOr 循环固定单个值, 用target - 固定值, 用二分法查待匹配的值。def two_sum_3(nums, target):    # 列表有序,初始值 Sum = Num[low]+Num[high]    # 从最左和最右移动索    low = 0    high = len(nums) - 1    while low < high:        sum = nums[low] + nums[high]  # 列表有序,初始 sum = 列表最小值+最大值        if sum > target:            high -= 1        elif sum < target:            low += 1        else:            return low, high    return -1, -1print(two_sum_3(nums, 22))# TODO:尝试目标值由三个数相加得来,返回三个下标。
二分法实例

 

转载于:https://www.cnblogs.com/adamans/articles/7570873.html

你可能感兴趣的文章
《C程序设计语言》练习1-5
查看>>
$\frac{dy}{dx}$ 是什么意思?
查看>>
Go开发之路(目录)
查看>>
RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法
查看>>
(50)与magento集成
查看>>
Ubuntu设置python3为默认版本
查看>>
JsonCpp 的使用
查看>>
问题账户需求分析
查看>>
JavaSE-代码块
查看>>
爬取所有校园新闻
查看>>
32、SpringBoot-整合Dubbo
查看>>
python面向对象基础
查看>>
HDU 2044 一只小蜜蜂(递归)
查看>>
docker 下 安装rancher 笔记
查看>>
spring两大核心对象IOC和AOP(新手理解)
查看>>
数据分析相关
查看>>
Python LDAP中的时间戳转换为Linux下时间
查看>>
微信小程序蓝牙连接小票打印机
查看>>
环境错误2
查看>>
C++_了解虚函数的概念
查看>>