当前位置:网站首页>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).
边栏推荐
- Eat a rich woman's melon...
- Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
- [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
- Daily mathematics serial 55: February 24
- 大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
- Module 9: design e-commerce seckill system
- 678. Valid bracket string
- 106. 从中序与后序遍历序列构造二叉树
- 12.Gateway新一代网关
- Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
猜你喜欢

Fried money, lost 10million.

The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion

TC8:UDP_USER_INTERFACE_01-08

I like two men...

Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?

京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...

Module 9: design e-commerce seckill system

What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in

Apple amplification! It's done so well

持续进阶,软通动力稳步推动云智能战略
随机推荐
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
持续进阶,软通动力稳步推动云智能战略
关于OpenCV中图像的widthStep
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
[laravel] detailed explanation of faker data filling
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
Kotlin 协程调度切换线程是时候解开真相了
Design and practice of new generation cloud native database
树莓派4B系统搭建(超详细版)
SQL 化是 ETL 增量生产的第一步,这样的架构的核心能力是什么?
C# 一行代码计算文件的MD5值 - CodePlus系列
678. 有效的括号字符串
Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
prism journal导航按钮的可用性探索记录
SQL server2014 failed to delete the database, with an error offset of 0x0000
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
线程基础知识
What legal risks and qualifications should be paid attention to when building a digital collection platform?
SQL optimization - in and not in, exist