442. 数组中重复的数据

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?

给定一个整数数组 a,其中 1 ≤ a[i] ≤ n(n 为数组长度),其中有些元素出现两次而其他元素出现一次

找到所有出现两次的元素。

你可以不用到任何额外空间并在 O(n) 时间复杂度内解决这个问题吗?

每日一算法2019/5/20Day 17LeetCode442. Find All Duplicates in an Array

示例:

输入:
[4,3,2,7,8,2,3,1]

输出:

[2,3]

Java 实现

import java.util.ArrayList;
import java.util.List; class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
int index = Math.abs(nums[i]) - 1;
if (nums[index] < 0) {
res.add(Math.abs(index + 1));
}
nums[index] = -nums[index];
}
return res;
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; ++i) {
if (!set.add(nums[i])) {
res.add(nums[i]);
}
}
return res;
}
}

相似题目

参考资料

LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17的更多相关文章

  1. Java实现 LeetCode 442 数组中重复的数据

    442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...

  2. [Swift]LeetCode442. 数组中重复的数据 | 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 ...

  3. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  4. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  5. 442 Find All Duplicates in an Array 数组中重复的数据

    给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...

  6. LeetCode - 统计数组中的元素

    1. 统计数组中元素总结 1.1 统计元素出现的次数 为了统计元素出现的次数,我们肯定需要一个map来记录每个数组以及对应数字出现的频次.这里map的选择比较有讲究: 如果数据的范围有限制,如:只有小 ...

  7. 关于iOS去除数组中重复数据的几种方法

    关于iOS去除数组中重复数据的几种方法   在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...

  8. php去除数组中重复数据

    <?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...

  9. php获取数组中重复数据的两种方法

    分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...

随机推荐

  1. 《挑战30天C++入门极限》C++的iostream标准库介绍(3)

        C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...

  2. SVN优于CVS之处

    1.原子提交.一次提交不管是单个还是多个文件,都是作为一个整体提交的.在这当中发生的意外例如传输中断,不会引起数据库的不完整和数据损坏. 2.重命名.复制.删除文件等动作都保存在版本历史记录当中. 3 ...

  3. Java Redis缓存穿透/缓存雪崩/缓存击穿,Redis分布式锁实现秒杀,限购等

    package com.example.redisdistlock.controller; import com.example.redisdistlock.util.RedisUtil; impor ...

  4. <ImageFieldFile:XXXX> is not JSON serializable

    问题描述: 使用django.forms.model下的model_to_dict来序列化ImageFieldFile出现不能序列化错误 使用json.dumps会出现同样的情况 解决办法: 方法一: ...

  5. Docker配置文件详解

    先来看一份 docker-compose.yml 文件,不用管这是干嘛的,只是有个格式方便后文解说: version: '2' services: web: image: dockercloud/he ...

  6. 2018-2019-2 《网络对抗技术》Exp9 WebGoat 20165326

    Web安全基础 jar包,密码:9huw 实验问题回答 SQL注入攻击原理,如何防御 原理:恶意用户在提交查询请求的过程中将SQL语句插入到请求内容中,同时程序本身对未对插入的SQL语句进行过滤,导致 ...

  7. sqlite3 查看Cookie文件<转>

    原帖地址:https://blog.csdn.net/yygydjkthh/article/details/37604309 .使用命令对此数据库进行打开: $ sqlite3 cookies.sql ...

  8. useMemo优化React Hooks程序性能(九)

    useMemo主要用来解决使用React hooks产生的无用渲染的性能问题.使用function的形式来声明组件,失去了shouldCompnentUpdate(在组件更新之前)这个生命周期,也就是 ...

  9. firewalld 实现nat 转发外网转发到内网 实现访问外网端口到内网

    第0步骤:  开启转发生效 [root@node178.oldboy.com ~]# cat /proc/sys/net/ipv4/ip_forward1 [root@node178.oldboy.c ...

  10. linux: 右键添加打开终端

    安装一个包,即可在右键里面添加一个“打开终端”的菜单. sudo apt-get install nautilus-open-terminal 注销用户重启,然后再进入就可以右键->在终端打开选 ...