当前位置:网站首页>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).
边栏推荐
- C# 一行代码计算文件的MD5值 - CodePlus系列
- SQL server2014 failed to delete the database, with an error offset of 0x0000
- 数字藏品新一轮热度开启
- What legal risks and qualifications should be paid attention to when building a digital collection platform?
- Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
- Superscalar processor design yaoyongbin Chapter 4 branch prediction -- Excerpt from subsection 4.1
- SQL 化是 ETL 增量生产的第一步,这样的架构的核心能力是什么?
- IDEA运行报错Command line is too long. Shorten command line for...
- I like two men...
- CRC check
猜你喜欢

关于#SQL#的问题,如何解决?

预制菜迎来“黄金时代”,谁能领跑下一个万亿市场

缺少比较器,运放来救场!(运放当做比较器电路记录)

基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建

uniapp微信小程序组件按需引入
![C [byte array] and [hexadecimal string] mutual conversion - codeplus series](/img/d2/dad88f53701c7cd7638bd4983cbb4b.png)
C [byte array] and [hexadecimal string] mutual conversion - codeplus series

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

【MPC】②quadprog求解正定、半正定、负定二次规划

日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利

If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
随机推荐
Write your own who commands
venv: venv 的目录结构
Error: missing revert data in call exception
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
sql语句修改字段类型「建议收藏」
Thread Basics
基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
Finally, someone made it clear what DRAM and NAND flash are
机器学习之线性回归详解
prism journal导航按钮的可用性探索记录
What is cloud primordial? Will it be the trend of future development?
Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
【Laravel 】faker数据填充详解
【邂逅Django】——(二)数据库配置
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
In the new database era, don't just learn Oracle and MySQL
Sleeping second brother...
SQL statement modify field type "suggestions collection"
SQL 化是 ETL 增量生产的第一步,这样的架构的核心能力是什么?
About widthstep of images in opencv