当前位置:网站首页>[sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
[sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
2022-06-30 17:40:00 【LuZhouShiLi】
The finger of the sword Offer 53 - II. 0~n-1 Missing numbers in
subject
A length of n-1 All numbers in the incremental sort array of are unique , And every number is in the range 0~n-1 within . In scope 0~n-1 Internal n There are and only one number is not in the array , Please find out the number .
Ideas
Use binary search , If the value of the intermediate element is equal to the subscript , Then the next round of search only needs to find the right half ; If the value and subscript of the intermediate element don't want to wait , And the preceding element is equal to its subscript , This means that the middle number is exactly the first value and the element whose subscript doesn't want to wait , Its subscript is a number that does not exist in the array ; If the value of the intermediate element and the subscript are not equal , And the element before it is not equal to its subscript , That means that in the next round of search, we only need to search in the left half .
Code
class Solution {
public:
int missingNumber(vector<int>& nums) {
int left = 0;
int right = nums.size() - 1;
while(left <= right)
{
int mid = (left + right) / 2;
// If equal explain Unequal elements on the right
if(nums[mid] == mid)
{
left = mid + 1;// Look to the right
}
else
{
right = mid - 1;// It's not equal Indicates that unequal elements are on the left
}
}
return left;
}
};
边栏推荐
- 以往我们认识的产业互联网,只是以消费互联网的替代者的身份出现
- [proteus simulation] Arduino uno uses 74ls148 to extend interrupt
- 墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
- Exch: repair the missing system mailbox
- 美国穆格moog伺服阀D661-4577C
- Is there an optimal solution to the energy consumption anxiety in the data center?
- NielsenIQ迎来零售实验室负责人Dawn E. Norvell,将加速扩张全球零售战略
- Shutter music recording playing audioplayers
- Login box tricks
- Horizontal visual error effect JS special effect code
猜你喜欢

Cesium-1.72 learning (camera tracking)

数据分析新动力——国内首款开源一体化实时HTAP数据库石原子StoneDB发布

canvas云朵形状动画

China Infrastructure Development Association: electronic contract is recommended

ROC-RK3566-PC使用10.1寸IPS触摸屏显示
![[Architecture] 1366- how to draw an excellent architecture diagram](/img/98/5dc29e08e91e751f67d910fadc6430.jpg)
[Architecture] 1366- how to draw an excellent architecture diagram

Mysql8 NDB cluster installation and deployment

期未课程设计:基于SSM的产品销售管理系统
![[C language] explain threads - thread separation function pthread_ detach](/img/d7/cce6c8afda85376eb569b1a5239c48.png)
[C language] explain threads - thread separation function pthread_ detach

广电5G正式启航,黄金频段将如何应用引关注
随机推荐
svg实现的订票UI效果
Cesium-1.72 learning (earth rotation)
k线图精解与实战应用技巧(见位进场)
MySQL之零碎知识点
Parker Parker sensor p8s-grflx
【剑指Offer】52. 两个链表的第一个公共节点
3D chart effectively improves the level of large data screen
Map collection
浅析搭建高速公路视频监控平台的建设方案及必要性
万卷书 - 书单整理 [01]
Parker proportional overflow valve rs10r35s4sn1jw
广电5G正式启航,黄金频段将如何应用引关注
Key to understanding the trend of spot Silver
Map集合
5G业务正式商用,属于广电的机会在哪?
泰山OFFICE技术讲座:同一行不同字号的字如何对齐绘制
Building a basic buildreoot file system
Network: principle and practice of server network card group technology
Hyper-v:在虚拟网络中启用 SR-IOV
巴比特 | 元宇宙每日必读:未成年人打赏后要求退款,虚拟主播称自己是大冤种,怎么看待这个监管漏洞?...