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) 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ...
随机推荐
- 对多条件进行组合,生成笛卡尔积的用例集合的python代码实现
做专项测试需要对一些因素进行组合的测试,这里组合起来后数据量可能很大,我们可以用python来代劳 代码有优化空间,目前先用着. ************************代码开始******* ...
- VSC 解决红底线问题
话不多说 设置里代码奉上 { "editor.minimap.enabled": false, "workbench.iconTheme": "vs ...
- 超链接标签绑定JS事件&&不加"javascript:;"导致的杯具
很久以来,在写Html和JS时,经常会给超链接<a>标签,绑定JS事件. 我们经常看到这样的写法,<a href="javascript:;" onclick=& ...
- 史上最全的Spring-Boot-Starter开发手册
1.前面的话 我们都知道可以使用 SpringBoot 快速的开发基于 Spring 框架的项目.由于围绕 SpringBoot 存在很多开箱即用的 Starter 依赖,使得我们在开发业务代码时能够 ...
- linux-高并发与负载均衡-lvs-DR模型试验
先配置3台虚拟机的网络 3台虚拟机克隆的方法:(....) etho,配置在同一个网段 DIP,RIP在一个网段 node01:作为lvs负载均衡服务器 node02:作为 Real Server n ...
- 手动执行脚本可以运行,crontab自动执行无效的解决方法
在需要执行的脚本里加入环境变量即可,如下图:
- 运行错误:Application Error - The connection to the server was unsuccessful
在模拟器上上启动ionic4.6版本 打包成的android APK,启动了很久结果弹出这个问题: Application Error - The connection to the server w ...
- js判断iPhone XS、iPhone XS Max、iPhone XR
// iPhone X.iPhone XS && window.screen.width === && window.screen.height === ; // iP ...
- JavaScript继承总结
1.创建对象 1.字面量对象 2.构造函数 3.Object.create //1.字面量 var obj={ name: '字面量', show: function(){ console.log(t ...
- java常用类-上
一,常用类之一包装类 java开发中习惯把八大基本数据类型封装到一个类中,并提供属性和方法,更方便的操作基本数据类型. 包装类的出现并不是用于取代基本数据类型,也取代不了. 包装类位于java.lan ...