当前位置:网站首页>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;
}
}
边栏推荐
- AddressSanitizer 技术初体验
- Color is converted to tristimulus value (r/g/b) (dry stock)
- 新一代垃圾回收器—ZGC
- 方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
- 精彩编码 【进制转换】
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
- Tencent architects first, 2022 Android interview written examination summary
- AsyncHandler
猜你喜欢
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
New generation garbage collector ZGC
某东短信登录复活 安装部署教程
beegfs高可用模式探讨
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
腾讯T2大牛亲自讲解,跳槽薪资翻倍
系统与应用监控的思路和方法
PowerPivot - DAX (first time)
【计网】第三章 数据链路层(3)信道划分介质访问控制
随机推荐
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
腾讯字节阿里小米京东大厂Offer拿到手软,老师讲的真棒
【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
beegfs高可用模式探讨
Tencent T2 Daniel explained in person and doubled his job hopping salary
报错分析~csdn反弹shell报错
JS get browser system language
Deep learning classification network -- zfnet
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
Configuration and simple usage of the EXE backdoor generation tool quasar
小微企业难做账?智能代账小工具快用起来
Introduction of Xia Zhigang
[Yann Lecun likes the red stone neural network made by minecraft]
An East SMS login resurrection installation and deployment tutorial
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
BUUCTF---Reverse---easyre
Groovy基础语法整理
数据的同步为每个站点创建触发器同步表
RT thread I2C tutorial