LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17
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;
}
}
相似题目
参考资料
- https://leetcode.com/problems/find-all-duplicates-in-an-array/
- https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/
LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17的更多相关文章
- Java实现 LeetCode 442 数组中重复的数据
442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...
- [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 ...
- Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- 442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...
- LeetCode - 统计数组中的元素
1. 统计数组中元素总结 1.1 统计元素出现的次数 为了统计元素出现的次数,我们肯定需要一个map来记录每个数组以及对应数字出现的频次.这里map的选择比较有讲究: 如果数据的范围有限制,如:只有小 ...
- 关于iOS去除数组中重复数据的几种方法
关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...
- php去除数组中重复数据
<?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...
- php获取数组中重复数据的两种方法
分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...
随机推荐
- Windowns下code: command not found
错误信息:code: command not found’ 解决办法:配置VsCode环境变量 首先找到VsCode点击其属性,查看路径 进入这个路径后 将D:\Program Files\Micro ...
- SyntaxError: Non-ASCII character 'æ' in file csdn.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
错误信息: SyntaxError: Non-ASCII character , but no encoding declared; see http://python.org/dev/peps/pe ...
- mysql命令行修改密码
1.以管理员身份打开cmd窗口2.进入安装mysql的bin目录.3.命令net start mysql开启服务4.mysql -u root -p 管理员登陆5.输入旧密码后进入数据库6.输入ALT ...
- appium+python 微信小程序的自动化
sudo kill -9 $(lsof -i:8889 -t) mitmweb -p 8889 -s addons.py mitmdump -q -p 8889 -s addons.py http: ...
- Java利用FastJson一行代码转List<Map>为List<Bean>
/** * 字符串 -> json对象.实体对象.Map.List.List<Map> */ // 字符串 -> json对象 JSONObject json = JSON.p ...
- MySQL - \g 和 \G用法与区别
[1]DOS环境下 ① \g 可同时(单独)使用\g; 其作用等效于分号—’:’ : ② \G 可同时(单独)使用\G;; /G 的作用是将查到的结构旋转90度变成纵向:
- MiniUI表单验证总结
原文地址:https://www.cnblogs.com/wllcs/p/5607890.html 1,页面效果图 2,代码实现 <!DOCTYPE html PUBLIC "-/ ...
- Java基础 Scanner 使用nextInt接收整数
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- ansible常用的方法小结
一.批量安装zabbix客户端 .拷贝sh脚本和.conf到远程服务器(也可以全量拷贝客户端) ansible all -m copy -a "src=/usr/local/zabbix_a ...
- mybatis xml <choose>标签使用
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...