当前位置:网站首页>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;
}
}
边栏推荐
- Database specific interpretation of paradigm
- Discussion on beegfs high availability mode
- 持续测试(CT)实战经验分享
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- AsyncHandler
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- 爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
- POJ1149 PIGS 【最大流量】
- Case ① | host security construction: best practice of 3 levels and 11 capabilities
- Qinglong panel white screen one key repair
猜你喜欢

爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother

5. Nano - Net in wireless body: Top 10 "is it possible?" Questions

Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution

01 基础入门-概念名词

【GET-4】

Discussion on beegfs high availability mode

Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022

【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN

Transformer model (pytorch code explanation)
随机推荐
mod_wsgi + pymssql通路SQL Server座
POJ3617 Best Cow Line 馋
数据的同步为每个站点创建触发器同步表
Node. Js: express + MySQL realizes registration, login and identity authentication
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Poj1149 pigs [maximum flow]
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
腾讯T3手把手教你,真的太香了
精彩编码 【进制转换】
Redisson bug analysis
New generation garbage collector ZGC
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Wechat applet common collection
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
Introduction of Xia Zhigang
Introduction to enterprise lean management system
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
golang的超时处理使用技巧
Tencent T3 teaches you hand in hand. It's really delicious
Logstash expressway entrance