当前位置:网站首页>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;
}
}
边栏推荐
- PowerPivot - DAX (first time)
- Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
- Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 【GET-4】
- 永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
- POJ 3207 Ikki&#39; s Story IV – Panda&#39; s Trick (2-SAT)
- Period compression filter
- Color is converted to tristimulus value (r/g/b) (dry stock)
- 腾讯T4架构师,android面试基础
猜你喜欢
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
Example of applying fonts to flutter
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Notes on beagleboneblack
beegfs高可用模式探讨
Enumeration gets values based on parameters
Tencent T2 Daniel explained in person and doubled his job hopping salary
腾讯架构师首发,2022Android面试笔试总结
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
随机推荐
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
mod_ WSGI + pymssql path SQL server seat
腾讯T2大牛亲自讲解,跳槽薪资翻倍
Leetcode brush first_ Maximum Subarray
JVM_常见【面试题】
【计网】第三章 数据链路层(3)信道划分介质访问控制
golang的超时处理使用技巧
Deep learning classification network -- zfnet
Error analysis ~csdn rebound shell error
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
AddressSanitizer 技术初体验
Problems encountered in using RT thread component fish
Groovy基础语法整理
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
[cloud native and 5g] micro services support 5g core network
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
SSH connection denied
BeagleBoneBlack 上手记
A5000 vGPU显示模式切换
Node.js: express + MySQL实现注册登录,身份认证