LeetCode Find All Duplicates in an Array
原题链接在这里:https://leetcode.com/problems/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?
Example:
Input:
[4,3,2,7,8,2,3,1] Output:
[2,3]
题解:
类似Find All Numbers Disappeared in an Array. iterate nums array时把nums[Math.abs(nums[i]-1)]标负,但需要先检查是否已经标过负了。若是说明是出现过一次. 把index+1添加到res中.
Time Complexity: O(nums.length). Space: O(1).
AC Java:
public class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
if(nums == null || nums.length == 0){
return res;
} for(int i = 0; i<nums.length; i++){
int index = Math.abs(nums[i])-1;
if(nums[index] < 0){
res.add(index + 1);
}else{
nums[index] = -nums[index];
}
}
return res;
}
}
可以吧num[i] swap到对应的index = nums[i]-1上面.
第二遍iterate时如果nums[i] !=i+1. nums[i]就是duplicate的. 加入res中.
Time Complexity: O(n). Space: O(1), regardless res.
AC Java:
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i<nums.length; i++){
if(nums[i]-1>=0 && nums[i]-1<nums.length && nums[i]!=nums[nums[i]-1]){
swap(nums, i, nums[i]-1);
i--;
}
} for(int i = 0; i<nums.length; i++){
if(nums[i] != i+1){
res.add(nums[i]);
}
} return res;
} private void swap(int [] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
LeetCode Find All Duplicates in an Array的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- Swift - UITableView里的cell底部分割线左侧靠边
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...
- 监测程序运行的时间,stopWatch
ArrayList arrInt = new ArrayList(); //用stopwatch来计时 运行的时间 Stopwatch watch = new Stopwatch(); watch.S ...
- Class.forName()的作用
大家都用过Class.forName(),也都知道是类加载的作用,其实这方法不只是类加载,还有类初始化. 下面用个小例子说明一下: A类,是用来加载的类 /** * 用来测试类加载的类此类有 * 静态 ...
- 【转】png优化相关
Png是图像文件存储格式,在网页设计中已经不是一个陌生的名词,在前端开发中经常使用到它,如常用CSS 雪碧图.而Png的使用不仅仅如此,Png有多少种格式,有哪些特点,PC端中常用的Png格式是哪些, ...
- [Unity3D]Unity+Android交互教程——让手机"动"起来
想要用Unity实现一个二维码扫描的功能,然后网上找插件,找到一个貌似叫EasyCodeScanner,但下载下来用用,真不好使,一导入运行就报错,调好错了再运行发现点按钮没反应,反复试了几遍发现还是 ...
- redis数据类型之—Set
(1)set 简单介绍 set集合的数据是不重复的.无序的,一个集合类型键可以存储至多2^32-1 个字符串. (2)set 常用命令
- iOS中获取各种文件的目录路径的方法
我们的app在手机中存放的路径是:/var/mobile/Applications/4434-4453A-B453-4ADF535345ADAF344 后面的目录4434-4453A-B453-4AD ...
- 理解callback function in javascript
以下内容主要摘自[1,2] (1)In javascript, functions are first-class objects, which means functions can be used ...
- WebRTC APM音频处理流程概述
本文主要介绍WebRTC的APM. 现在主要介绍一下audio_processing.h. 首先插入了几个类,这些都是audio_processing的核心模块. class AudioFrame; ...
- Java整型与字符串相互转换
>>>>>>>>>>>>>>>>>>>> 1如何将字串 String 转换成整数 ...