当前位置:网站首页>二分查找
二分查找
2022-07-24 03:06:00 【算法与编程之美】
引言
二分查找是基础算法的一种,有利于我们提高查找效率。问题
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
示列1:
输入:nums = [-1, 0, 3, 5, 9, 12, 54, 60, 100], target = 9
输出:4
示例2:
输入:nums = [-1, 0, 3, 5, 12, 54, 60, 100], target = 9
输出:-1方法
应为该数组为升序数组。首先,设l,r分别为该数组第一个下标和最后一个,在找到中间值的下标mid = (l + r)// 2,最后将其进行比较,最后找到要找的target。实验结果与讨论
| def f(nums): l, r = 0, len(nums) - 1 while l < r: mid = (l + r) // 2 if nums[mid] >= target: r = mid else: l = mid + 1 return r |
5.结语
这是最典型的二分查找的运用,二分查找对于类似问题可以提高效率,但二分查找只能用于有序排列的数组,不能对无序数组进行操作。
边栏推荐
- Ugui source code analysis - stencilmaterial
- Example of producer consumer code implemented by the destructor framework without lock
- Unity message push
- The first edition of Niuke brush question series (automorphic number, return the number of prime numbers less than N, and the first character only appears once)
- go IO操作-文件读
- Mobile keyboard (day 73)
- Basic knowledge of trigger (Part 2)
- Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
- 攻防世界WEB练习区(weak_auth、simple_php、xff_referer)
- The solution of using non root user management in secure stand-alone database under general machine environment
猜你喜欢
![Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]](/img/7d/c309b7b0b919df44b5c1461d79e7e7.png)
Hospital PACS source code PACS ultrasonic Department source code DICOM image workstation source code [source code free sharing]

Relational expression greater than > less than < congruence = = = Nan isnan() logical operator double sense exclamation point!! & |% +-- Short circuit calculation assignment expression shortcut operat

Ugui source code analysis - stencilmaterial

The function of SIP account - tell you what is SIP line

Analyze the space practice field required by maker education activities

summernote富文本编辑器

In the future, when the interviewer asks why you don't recommend using select *, please answer him loudly!

Attack and defense world web practice area (backup, cookie, disabled_button)

Honey, we are homeless now

I developed an app similar to wechat runnable applet with fluent
随机推荐
Lcd1602——斌哥51
Minimum exchange times
SSM's technical forum includes front and back offices
移动通信的新定义:R&SCMX500 将提高5G设备的IP数据吞吐量
Openresty Lua resty balancer dynamic load balancing
Customize the default width and height of kindeditor rich text
The function of SIP account - tell you what is SIP line
How to realize software function safety
Unity 消息推送
Hcip --- BGP comprehensive experiment
LCD1602 - binge 51
go log包
Tweenmax+svg Pikachu transformation ball
Symbol type
SSM based blog system [with background management]
【英雄哥七月集训】第 23天: 字典树
[AMC] federal quantification
Redux Usage Summary
Diversity of SIGIR '22 recommendation system papers
攻防世界WEB练习区(webshell、command_execution、simple_js)