[LC] 442. Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:
[4,3,2,7,8,2,3,1] Output:
[2,3]
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i< nums.length; i++) {
// map nums[i] to index
int index = Math.abs(nums[i]) - 1;
if (nums[index] < 0) {
res.add(index + 1);
}
nums[index] = -nums[index];
}
return res;
}
}
[LC] 442. Find All Duplicates in an Array的更多相关文章
- leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array
后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...
- 442. Find All Duplicates in an Array - LeetCode
Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...
- LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素
[抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...
- 442. Find All Duplicates in an Array
https://leetcode.com/problems/find-all-duplicates-in-an-array/ 一列数,1 ≤ a[i] ≤ n (n = size of array), ...
- leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项
https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...
- LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)
题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...
- 442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...
随机推荐
- Thread--生产者消费者
2个生产者,2个消费者,库存容量2 package p_c_allWait.copy; import java.util.LinkedList; import java.util.List; publ ...
- 18 11 15 网络通信 ---- 多任务----线程 threading
下面是一个 多线程 运算 调用了 threading 模块 可以同时在一个程序中 跑两个函数 import threading def text1 (): for i in range( ...
- mysql6数据库安装与配置
详细步骤可以参考这篇文章 https://www.cnblogs.com/duguangming/p/10623520.html 1.下载并打开mysql6数据库安装文件 2.默认点击下一步 3.点击 ...
- JDBC-使用Java连接数据库-基础篇
这是小主第一次写Java连接数据库博客,初学Java之时,Java连接数据库是我最头疼的,不过经过一个月的学习,也差不多略有收获,所以给大家分享一下. Java连接数据库大约需要五大步骤: 创建数据库 ...
- KVM---虚拟机网络管理
在上篇博客中我们完成了 KVM 虚机的安装,但是我发现虚机内的网络是不通的(当然了,在写这篇博客的时候已经把上篇博客中的配置文件修改好了,网络也是通的了,嘻嘻),所以这篇博客总结了一下虚机的网络连接方 ...
- PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]
题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...
- geodjango七日学习笔记 (7.30整理本地笔记上传到网络)
第一天进行到现在,在开端的尾巴,想起来写一个学习笔记, 开发环境已搭好,用的是pycharm 环境是本机已有的interpreter python3.7 接下来要做的是新建一个geodjango项 ...
- JavaSE--异常信息打印
最近项目用到第三方jar包,抛出运行时异常,打在日志用的 方法.得到的错误描述并不详尽,遂想到平时用的 发现其可以重定向输出,平时用流多是和文件相关,但是在当前背景下用文件打开流显得不是很合适,翻了下 ...
- Python操作redis总结
安装模块及配置 首先安装redis,在Ubuntu下输入指令pip install redis即可.下载完成后,cd到指定目录下,打开指定文件,如下图所示: 输入密码打开后,修改指定地方的内容,与上篇 ...
- HDU-6514 Monitor(二维前缀和+差分)
http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land ...