当前位置:网站首页>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;
}
}
边栏推荐
- Introduction of Xia Zhigang
- Introduction to enterprise lean management system
- 腾讯架构师首发,2022Android面试笔试总结
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- Qinglong panel white screen one key repair
- 数据的同步为每个站点创建触发器同步表
- B-杰哥的树(状压树形dp)
- Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
猜你喜欢
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
某东短信登录复活 安装部署教程
枚举根据参数获取值
数字三角形模型 AcWing 1018. 最低通行费
腾讯T2大牛亲自讲解,跳槽薪资翻倍
Error analysis ~csdn rebound shell error
【GET-4】
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
随机推荐
Example of applying fonts to flutter
Guangzhou's first data security summit will open in Baiyun District
【GET-4】
【云原生与5G】微服务加持5G核心网
深度学习分类网络 -- ZFNet
02 基础入门-数据包拓展
腾讯T3手把手教你,真的太香了
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
Color is converted to tristimulus value (r/g/b) (dry stock)
Notes on beagleboneblack
Web开发小妙招:巧用ThreadLocal规避层层传值
In simple terms, interview surprise Edition
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
POJ3617 Best Cow Line 馋
微信小程序常用集合
某东短信登录复活 安装部署教程
Initial experience of addresssanitizer Technology
8086指令码汇总表(表格)