当前位置:网站首页>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 :

边栏推荐
- Sword finger offer 29 Print matrix clockwise
- How to turn off debug information in rtl8189fs
- MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
- Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
- Remote connection to MySQL under windows and Linux system
- Mathematics in Sinorgchem: computational geometry
- Calculation (computer) code of suffix expression
- DNS domain name resolution
- [untitled]
- 2022 low voltage electrician test question simulation test question bank simulation test platform operation
猜你喜欢

【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表

How to hide the scroll bar of scroll view in uniapp

【liuyubobobo-玩转Leetcode算法面试】【00】课程概述

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

Pytest testing framework

结婚后

Jvm-01 (phased learning)

Software development life cycle -- waterfall model
![[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)](/img/e0/05890eafdb291c5aaff78cc241f455.jpg)
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)

After marriage
随机推荐
Pytest testing framework
MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
pytest 测试框架
es面試題
Provincial election + noi Part IV graph theory
PHP notes - use Smarty to set public pages (include, if, else, variable settings)
Divorce for 3 years to discover the undivided joint property, or
Stack - es - official documents - filter search results
Leetcode question brushing (10) - sequential question brushing 46 to 50
Multi threaded query, double efficiency
how to add one row in the dataframe?
New programmer magazine | Li Penghui talks about open source cloud native message flow system
附加:信息脱敏;
Flutter un élément au milieu, l'élément le plus à droite
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
软件开发生命周期 --瀑布模型
C return multiple values getter setter queries the database and adds the list return value to the window
Sword finger offer 47 Maximum value of gifts
【OpenCV】-5种图像滤波的综合示例
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)