Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
示例:
输入:
[4,3,2,7,8,2,3,1]
输出:
[2,3]
思路
思路一:
直接利用hashmap记录出现次数
思路二:
因为数组输入的特点 1<=a[i]<=n,则可以把原数组当hash表用 ,因为原数组是正数,标为负数表示出现过,如果遇到负数就表示第二次出现,就可以找出所有出现过两次的元素
代码实现
package Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 442. Find All Duplicates in an nums(数组中重复的数据)
* 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
* 找到所有出现两次的元素。
*/
public class Solution442 {
public static void main(String[] args) {
Solution442 solution442 = new Solution442();
int[] nums = {4, 3, 2, 7, 8, 2, 3, 1};
List<Integer> res = solution442.findDuplicates(nums);
System.out.println(res.toString());
}
/**
* 直接利用hashmap记录出现次数
*
* @param nums
* @return
*/
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
HashMap<Integer, Integer> map = new HashMap<>();
int count;
for (int val :
nums) {
if (!map.containsKey(val)) {
map.put(val, 1);
} else {
count = map.get(val);
map.put(val, ++count);
}
if (map.get(val) == 2) {
res.add(val);
}
}
return res;
}
/**
* 因为数组输入的特点 1<=a[i]<=n,则可以把原数组当hash表用 ,因为原数组是正数,标为负数表示出现过,如果遇到负数就表示第二次出现,就可以找出所有出现过两次的元素
*
* @param nums
* @return
*/
public List<Integer> findDuplicates_2(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;
}
}
Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)的更多相关文章
- 442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...
- 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 ...
- Java实现 LeetCode 442 数组中重复的数据
442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- [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 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
- Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array)
Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array) 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ...
随机推荐
- vue源码分析—认识 Flow
认识 Flow Flow 是 facebook 出品的 JavaScript 静态类型检查⼯具.Vue.js 的源码利⽤了 Flow 做了静态类型检查, 所以了解 Flow 有助于我们阅读源码 Flo ...
- 五 Struts 配置文件
一.struts.xml详解1.package:包 属性:name=命名 namespace=在action访问路径前面加的前缀 extends=继承另外的包 action:name=action的访 ...
- Python链表的实现与使用(单向链表与双向链表)
参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...
- NodeJs之服务搭建与数据库连接
NodeJs之服务搭建与数据库连接 一,介绍与需求分析 1.1,介绍 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻 ...
- LVS实现负载均衡原理
负载均衡集群是load balance 集群的简写.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备有F5.NetsNetscale.这里主要是学习lvs. === ...
- Struts2+Spring+Hibernate3整合
这几天正在复习三大框架的知识,特意把写出来,如有错误,希望大家多指教! 代码地址:https://git.coding.net/puchenglin/SSHDemo.git 1. 引入jar包 Str ...
- Vue组件开发
在学习vue的时候,发现有很多使用vue开发的ui组件.本着学习的目的,自己也仿照Element写一些组件. 使用VuePress编写组件文档. 单元测试:karma+mocha+chai+sinon ...
- Codeforces1153F Serval and Bonus Problem 【组合数】
题目分析: 我们思考正好被k个区间覆盖的情况,那么当前这个子段是不是把所有的点分成了两个部分,那么在两个部分之间相互连k条线,再对于剩下的分别连线就很好了?这个东西不难用组合数写出来. 然后我们要证明 ...
- mysql varcahr转int类型
cast(yysid as SIGNED INTEGER)
- CC++语法::数组名退化(array decaying)
参考: CSDN::C/C++中数组名退化为指针的情况 stackoverflow::What is array decaying? 起因 笔者在写memset的时候总想偷一点懒(因为我们一般都是为了 ...