当前位置:网站首页>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;
}
}
边栏推荐
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
- Oceanbase Community Edition OBD mode deployment mode stand-alone installation
- 【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
- 【云原生与5G】微服务加持5G核心网
- 技术分享 | 抓包分析 TCP 协议
- Tencent T3 teaches you hand in hand. It's really delicious
- (3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
- Tencent cloud database public cloud market ranks top 2!
- Deep learning classification network -- zfnet
猜你喜欢

某东短信登录复活 安装部署教程

Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行

Cesium Click to draw a circle (dynamically draw a circle)

Cesium 点击绘制圆形(动态绘制圆形)

【云原生与5G】微服务加持5G核心网

Case ① | host security construction: best practice of 3 levels and 11 capabilities

It's enough to read this article to analyze the principle in depth

Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing

Tencent Android interview must ask, 10 years of Android development experience

持续测试(CT)实战经验分享
随机推荐
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
腾讯云数据库公有云市场稳居TOP 2!
leetcode先刷_Maximum Subarray
Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
Redisson bug analysis
B-杰哥的树(状压树形dp)
5. 無線體內納米網:十大“可行嗎?”問題
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
报错分析~csdn反弹shell报错
22-07-05 upload of qiniu cloud storage pictures and user avatars
Cesium 点击绘制圆形(动态绘制圆形)
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
[network planning] Chapter 3 data link layer (3) channel division medium access control
Tencent T2 Daniel explained in person and doubled his job hopping salary
Introduction of Xia Zhigang
永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
夏志刚介绍