当前位置:网站首页>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;
}
}
边栏推荐
- Guangzhou's first data security summit will open in Baiyun District
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- Redisson bug analysis
- mod_wsgi + pymssql通路SQL Server座
- Learn to punch in Web
- 8086指令码汇总表(表格)
- Groovy基础语法整理
- 枚举根据参数获取值
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- Tencent cloud database public cloud market ranks top 2!
猜你喜欢

PowerPivot - DAX (first time)

Oceanbase Community Edition OBD mode deployment mode stand-alone installation

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅

OceanBase社区版之OBD方式部署方式单机安装

语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech

永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]
腾讯安卓开发面试,android开发的基础知识
腾讯T4架构师,android面试基础

Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
随机推荐
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
JVM_ Common [interview questions]
Catch ball game 1
Linear distance between two points of cesium
AsyncHandler
AsyncHandler
腾讯云数据库公有云市场稳居TOP 2!
mod_ WSGI + pymssql path SQL server seat
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
颜色(color)转换为三刺激值(r/g/b)(干股)
Groovy basic syntax collation
5. 无线体内纳米网:十大“可行吗?”问题
Learn to punch in Web
腾讯T4架构师,android面试基础
【GET-4】
Web开发小妙招:巧用ThreadLocal规避层层传值
8086 instruction code summary (table)
【GET-4】