当前位置:网站首页>442. duplicate data in array
442. duplicate data in array
2022-07-01 10:19:00 【hequnwang10】
One 、 Title Description
Give you a length of n Array of integers for nums , among nums All integers of are in the range [1, n] Inside , And each integer appears once or two . Please find out all that appear two The integer of , And return as an array .
You must design and implement a time complexity of O(n) And only the algorithm of constant extra space is used to solve this problem .
Example 1:
Input :nums = [4,3,2,7,8,2,3,1]
Output :[2,3]
Example 2:
Input :nums = [1,1,2]
Output :[1]
Example 3:
Input :nums = [1]
Output :[]
Two 、 Problem solving
One traverse
Sign judgment method , The numbers that appear are reversed , Then judge whether it has been reversed .
class Solution {
public List<Integer> findDuplicates(int[] nums) {
// This problem requires a traversal Then use the algorithm of extra space
int length = nums.length;
List<Integer> res = new ArrayList<>();
for(int i = 0;i<length;i++){
int num = Math.abs(nums[i]);
if(nums[num-1] > 0){
nums[num-1] = -nums[num-1];
}else{
res.add(num);
}
}
return res;
}
}
Time complexity :O(n)
Spatial complexity :O(1).
边栏推荐
- In the new database era, don't just learn Oracle and MySQL
- 在中金证券上做基金定投安全吗?
- Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
- What legal risks and qualifications should be paid attention to when building a digital collection platform?
- 有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
- 442. 数组中重复的数据
- CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
- Project0:小游戏
- SQL SERVER2014删除数据库失败,报错偏移量0x0000...
- Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
猜你喜欢

Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)

推荐一款 JSON 可视化工具神器!

数据库的增删改查问题

渗透常用工具-Goby

Error: missing revert data in call exception

SQL server2014 failed to delete the database, with an error offset of 0x0000

It is interesting to understand MMAP in this way!

A new round of popularity of digital collections opens

C one line code calculates the MD5 value of the file - codeplus series

SQL SERVER2014删除数据库失败,报错偏移量0x0000...
随机推荐
关于#数据库#的问题:GBase 8s中如何避免死锁
Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
Introduction of uniapp wechat applet components on demand
SQL statement modify field type "suggestions collection"
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
[laravel] detailed explanation of faker data filling
SQL server2014 failed to delete the database, with an error offset of 0x0000
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
Graduation summary of actual combat camp
CRC check
CCNP Part XII BGP (IV)
The stock position building rate of global funds and asset management reached a new low in 15 years
Floyd repeat
678. 有效的括号字符串
Japanese professor sues Intel FPGA and SOC products for infringing a design patent
How to solve the problem of SQL?
TC8:UDP_ USER_ INTERFACE_ 01-08
在通达信上买基金安全吗?
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul
12. Gateway new generation gateway