当前位置:网站首页>442. 数组中重复的数据
442. 数组中重复的数据
2022-07-01 10:08:00 【hequnwang10】
一、题目描述
给你一个长度为 n 的整数数组 nums ,其中 nums 的所有整数都在范围 [1, n] 内,且每个整数出现 一次 或 两次 。请你找出所有出现 两次 的整数,并以数组形式返回。
你必须设计并实现一个时间复杂度为 O(n) 且仅使用常量额外空间的算法解决此问题。
示例 1:
输入:nums = [4,3,2,7,8,2,3,1]
输出:[2,3]
示例 2:
输入:nums = [1,1,2]
输出:[1]
示例 3:
输入:nums = [1]
输出:[]
二、解题
一次遍历
正负号判断法,出现过的数字就数字取反,然后判断是否取反过。
class Solution {
public List<Integer> findDuplicates(int[] nums) {
//这题需要使用一次遍历 然后使用额外空间的算法
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;
}
}
时间复杂度:O(n)
空间复杂度:O(1)。
边栏推荐
- What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
- 请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
- 零基础入门测试该学什么?最全整理,照着学就对了
- Apple amplification! It's done so well
- Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
- 《天天数学》连载55:二月二十四日
- 睡了二哥。。。
- 关于#数据库#的问题:GBase 8s中如何避免死锁
- I like two men...
- IDEA运行报错Command line is too long. Shorten command line for...
猜你喜欢

PHP code audit and File Inclusion Vulnerability

Computer USB, HDMI, DP various interfaces and speeds

Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)

Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022

机器学习之线性回归详解

【黑马早报】俞敏洪称从来不看新东方股价;恒驰5将于7月开启预售;奈雪虚拟股票或涉嫌非法集资;7月1日起冰墩墩停产...

JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;

leetcode:111. 二叉树的最小深度

Continue to advance, and softcom power steadily promotes cloud intelligence strategy

Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
随机推荐
Continue to advance, and softcom power steadily promotes cloud intelligence strategy
button按钮清除边框
leetcode:111. 二叉树的最小深度
In the new database era, don't just learn Oracle and MySQL
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
Eat a rich woman's melon...
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
Apple amplification! It's done so well
Is it safe to buy funds on the access letter?
【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba
架构实战营 模块九:设计电商秒杀系统
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
苹果放大招!这件事干的太漂亮了……
【Laravel 】faker数据填充详解
Postgraduate entrance examination vocabulary 2023 sharing (1)
About widthstep of images in opencv
这样理解mmap,挺有意思!
Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability