当前位置:网站首页>Missing numbers from 0 to n-1 (simple difficulty)
Missing numbers from 0 to n-1 (simple difficulty)
2022-07-02 02:38:00 【Nulibiao】
Catalog
Title Overview ( Simple difficulty )

Ideas and code
Train of thought display
Sort the search problem in the array , think first of Dichotomy solve .
I didn't expect this topic to use dichotomy , Let's go directly to the code , It seems that I have done less
Code example
class Solution {
public int missingNumber(int[] nums) {
int i =0,j = nums.length - 1;
while(i <= j) {
int mid = (i+j)/2;
if(nums[mid] == mid) {
i= mid+1;
}else {
j = mid-1;
}
}
return i;
}
}
matters needing attention
1: Let's first look at the core idea
if(nums[mid] == mid) {
i= mid+1;
}else {
j = mid-1;
}
Because we have a length of n-1 Incremental sort array of , And every number is in the range 0~n-1 within , The meaning of this sentence is equivalent to that the value of the array subscript is equal to the value stored in the array , for example :nums[0]=0,nums[1]=1…
So when nums[mid] = mid When , It means that the numbers leading up to the subscript in our middle are the same as those of its subscript , Without us nums[mid] It can't be equal to mid Of , Then the missing numbers in our array can only be found in our mid After subscript , So we can discard half of the data every time , Every time it goes down 1/2 The scale of , Similar to a binary tree , The number of searches is the height of the binary tree , Therefore, the complexity is reduced to log2N --> O(logN). So let i=mid+1, contrary , If nums[mid] != mid, Explain that the number that is less can only be in our mid Before subscribing , At this point, we need j = mid-1
2:
Now our while In circulation i Yes, we must Less than or equal to j Of :
If it's less than j Words , At this time, the following test cases will fail :

边栏推荐
- [learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
- QT uses sqllite
- 结婚后
- 离婚3年以发现尚未分割的共同财产,还可以要么
- [untitled]
- 软件开发生命周期 --瀑布模型
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- C return multiple values getter setter queries the database and adds the list return value to the window
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
猜你喜欢

大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里

Webgpu (I): basic concepts

多线程查询,效率翻倍

pytest 测试框架

【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度

No programming code technology! Four step easy flower store applet
![[pit] how to understand](/img/e9/f5315a03b6f3da07021f915bb18af8.jpg)
[pit] how to understand "parameter fishing"
![[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the](/img/fe/d97b25f702bbc05f941d08147259e0.jpg)
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
![[opencv] - comprehensive examples of five image filters](/img/c7/aec9f2e03a17c22030d7813dd47c48.png)
[opencv] - comprehensive examples of five image filters

STM32__05—PWM控制直流电机
随机推荐
Sword finger offer 31 Stack push in and pop-up sequence
How to turn off the LED light of Rog motherboard
What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
trading
Es interview questions
The number one malware in January 2022: lokibot returned to the list, and emotet returned to the top
Feature query of hypergraph iserver rest Service
How to run oddish successfully from 0?
Summary of some experiences in the process of R & D platform splitting
flutter 中間一個元素,最右邊一個元素
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
How to batch add background and transition effects to videos?
Infix expression to suffix expression (computer) code
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
QT实现界面跳转
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
连通块模板及变式(共4题)
If you want to rewind the video picture, what simple methods can you use?
No programming code technology! Four step easy flower store applet
Set status bar color