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. [luogu 3803]【模板】多项式乘法

    传送门 FFT #include<bits/stdc++.h> #define ll long long #define max(a,b) ((a)>(b)?(a):(b)) #de ...

  2. LArea插件选中城市,确定之后又很难再次选择城市?

    加上fastclick.js这个js就能解决这个问题啦...... 详情:http://blog.csdn.net/zfy865628361/article/details/49512095

  3. Linux文件的权限的基本介绍

    一. ls  -l    显示的内容如下: 二.rwx权限详解 1.rwx作用到文件 2. rwx作用在目录 三.文件及目录实际案例 四.修改权限  -  chmod 1. 基本说明: 2.第一种方式 ...

  4. C#求任意两整数之和

    2019.9.11 作业要求: 求出任意两整数之和 解决方案: using System; using System.Collections.Generic; using System.Linq; u ...

  5. 面试题 int(3) int(10) 区别

    1.MySQL 中 int(3) int(10) 区别 答案 存储大小并无差异,只是不足位数的时候,左边补0. ###补充知识点 创建数据库表时,加zerofill ,可以看出效果.mysql 默认 ...

  6. Wrapper: Error - Unable to execute Java command

    在64位的系统下 将短信程序运行于服务中,出现以下错误: Error: [size=14px; line-height: 26px;]FATAL  | wrapper  | 2012/06/18 17 ...

  7. Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别

    Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别 区别: 这两个属性都属于user_tab_columns视图,他们的含义:1,da ...

  8. flutter中使用shared_preferences的存储

    添加依赖 shared_preferences: ^+ 工具类 import 'dart:async'; import 'package:shared_preferences/shared_prefe ...

  9. Ajax serialize()提交form表单不能上传file类型

    前台form表单的提交方式有很多种,例如: 1. form表单submit直接提交的方法 2. Ajax提交的方法 3. jquery提交的方法 4. 原生js提交的方法 每一种方法都有它的优势和不足 ...

  10. 004-行为型-01-策略模式(Strategy)

    一.概述 定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户.需要设计一个接口,为一系列实现类提供统一的方法,多个实现类实现该接口,设计一个抽象类(可有可 ...