LeetCode.442 Find All Duplicates in an Array
来源:互联网 发布:衣服品牌查询软件 编辑:程序博客网 时间:2024/05/21 10:35
题目:
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?
Example:
Input:[4,3,2,7,8,2,3,1]Output:[2,3]
class Solution { public List<Integer> findDuplicates(int[] nums) { //给定数组,长度为n,其中元素范围1-n,其中有些元素重复出现一次或者两次,找出所有重复的元素 //要求:在没有额外空间和运行时间不为O(n) //思路:用一个长度为n+1的数组记录各出现次数 List<Integer> list=new ArrayList<>(); //因为可能存在数值为n的元素,需要存入count[n]的下标中 int [] count=new int[nums.length+1]; for(int i=0;i<nums.length;i++ ){ count[nums[i]]++; if(count[nums[i]]==2){ list.add(nums[i]); } } return list; }}
阅读全文
0 0
- Leetcode 442 - Find All Duplicates in an Array (array)
- [LeetCode]Find All Duplicates in an Array
- LeetCode Find All Duplicates in an Array
- Leetcode Find All Duplicates in an Array
- [Leetcode] Find All Duplicates in an Array
- leetCode-Find All Duplicates in an Array
- [C语言][LeetCode][442]Find All Duplicates in an Array
- 【LeetCode-442】Find All Duplicates in an Array(C++)(
- leetcode 442 Find All Duplicates in an Array C++
- [LeetCode]442 Find All Duplicates in an Array
- LeetCode 442 --- Find All Duplicates in an Array
- [leetcode 442] Find All Duplicates in an Array
- LeetCode 442 Find All Duplicates in an Array
- Find All Duplicates in an Array,leetcode,442
- LeetCode.442 Find All Duplicates in an Array
- Leetcode 442-Find All Duplicates in an Array
- 【442】Find All Duplicates in an Array
- LeetCode-Find All Duplicates in an Array & Find All Numbers Disappeared in an Array
- BZOJ1419:Red is good(期望DP)
- Java中基本的逻辑运算和基本的原反补码的说明
- 等腰三角形的打印
- HTTP服务器简易版
- 用爬虫爬取京东物品的商品评价标签
- LeetCode.442 Find All Duplicates in an Array
- K:枚举的线程安全性及其序列化问题
- 文件的归档、压缩与传输
- Linux内核追踪[4.3] Ext3的终结
- 适配iphonex和ios11的相关问题总结
- 第七届蓝桥杯大赛个人赛省赛(软件类)java语言B组第二题:生日蜡烛
- 第8周项目3(3)
- 今天不加班,逛草榴去
- JAVA基本排序算法