当前位置:网站首页>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;
}
}
边栏推荐
- Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
- B-杰哥的树(状压树形dp)
- POJ1149 PIGS 【最大流量】
- [network planning] Chapter 3 data link layer (3) channel division medium access control
- Tencent Android interview must ask, 10 years of Android development experience
- 爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
- 【GET-4】
- 8086指令码汇总表(表格)
- Initial experience of addresssanitizer Technology
- BeagleBoneBlack 上手记
猜你喜欢

Discussion on beegfs high availability mode

Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
![[cloud native and 5g] micro services support 5g core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[cloud native and 5g] micro services support 5g core network
腾讯安卓开发面试,android开发的基础知识

永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
Tencent architects first, 2022 Android interview written examination summary
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development

Microservice architecture debate between radical technologists vs Project conservatives

持续测试(CT)实战经验分享

5. 無線體內納米網:十大“可行嗎?”問題
随机推荐
redisson bug分析
Continuous test (CT) practical experience sharing
Notes on beagleboneblack
Groovy基础语法整理
An East SMS login resurrection installation and deployment tutorial
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
Tencent T4 architect, Android interview Foundation
Linear distance between two points of cesium
SSH connection denied
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
How to handle the timeout of golang
深度学习分类网络 -- ZFNet
JVM_常见【面试题】
Wonderful coding [hexadecimal conversion]
【GET-4】
范式的数据库具体解释
JVM_ Common [interview questions]
In simple terms, interview surprise Edition