当前位置:网站首页>Leetcode question 448 Find all missing numbers in the array
Leetcode question 448 Find all missing numbers in the array
2022-07-06 20:14:00 【Invite the wind to intoxicate the moon】
448. Find all the missing numbers in the array
)
subject
Here's one for you n Array of integers nums , among nums[i] In the interval [1, n] Inside . Please find out all in [1, n] Range but not in nums Number in , And return the result in the form of array .
Example 1:
Input :nums = [4,3,2,7,8,2,3,1]
Output :[5,6]
Example 2:
Input :nums = [1,1]
Output :[2]
Tips :
n == nums.length
1 <= n <= 105
1 <= nums[i] <= n
Advanced : You can use no extra space and the time complexity is O(n) Solve this problem in the case of ? You can assume that the returned array is not included in the extra space .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer 1 : Use extra space for bucket sorting
Is to define an array a, The initial value is 0, Subscript to be i when ,a[i]++;nums[i] by 0, explain i It didn't show up
Explanation 2 : Modify in place ( Advanced )
“ Array elements -1” Become a new subscript , The element corresponding to the new subscript is modified , The unmodified subscript is the number that disappears
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n=nums.length;
for(int num : nums){
num =(num-1)%n;// Yes n Take the mold to restore the original value , Why is that ?
nums[num]+=n;
}
List<Integer> result =new ArrayList<Integer>();//ArrayList Of L Be sure to capitalize
for(int i=0;i<n;i++){
if(nums[i]<=n){
result.add(i+1);
}
}
return result;
}
}
边栏推荐
- mod_wsgi + pymssql通路SQL Server座
- Linear distance between two points of cesium
- PHP与EXCEL PHPExcel
- Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
- Groovy基础语法整理
- JS get browser system language
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- Cesium Click to draw a circle (dynamically draw a circle)
- 腾讯云数据库公有云市场稳居TOP 2!
- Cesium 两点之间的直线距离
猜你喜欢

02 基础入门-数据包拓展

Anaconda安装后Jupyter launch 没反应&网页打开运行没执行

腾讯T3手把手教你,真的太香了

Standardized QCI characteristics

数字三角形模型 AcWing 1018. 最低通行费

Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行

【计网】第三章 数据链路层(3)信道划分介质访问控制

Error analysis ~csdn rebound shell error
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]

22-07-05 upload of qiniu cloud storage pictures and user avatars
随机推荐
Leetcode brush first_ Maximum Subarray
深度学习分类网络 -- ZFNet
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
Tencent cloud database public cloud market ranks top 2!
Classic 100 questions of algorithm interview, the latest career planning of Android programmers
Discussion on beegfs high availability mode
8086指令码汇总表(表格)
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
B-杰哥的树(状压树形dp)
[Yann Lecun likes the red stone neural network made by minecraft]
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
报错分析~csdn反弹shell报错
腾讯T3大牛手把手教你,大厂内部资料
腾讯T3手把手教你,真的太香了
Microservice architecture debate between radical technologists vs Project conservatives
rt-thread i2c 使用教程
5. 无线体内纳米网:十大“可行吗?”问题
PowerPivot - DAX (first time)
AddressSanitizer 技术初体验