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?
Example:
Input:
[4,3,2,7,8,2,3,1] Output:
[2,3]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
只知道一个数重复两次就是画圈-快慢指针,很多数重复2次不知道怎么做
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
转一回:自己重复变成index重复,再转回到nums[index]重复。然后注意一下范围
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
判断重复需要:转一回:自己重复变成index重复,再转回到nums[index]重复。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public List<Integer> findDuplicates(int[] nums) {
//initialization
List<Integer> result = new ArrayList<Integer>(); //for loop: get the new index, add to result if negative, change to negative
for (int i = 0; i < nums.length; i++) {
//get the new index
int index = Math.abs(nums[i]) - 1; //add to result if negative
if (nums[index] < 0) result.add(index + 1); //change to negative
else nums[index] *= (-1);
}
//return
return result;
}
}
/*
[4, 3, 2, 7, 8, 2, 3, 1]
i 0 1 2 3 4 5 6 7
index 3 2 1
nums[index]*(-1) -7 -2 -3
*/
442. Find All Duplicates in an Array找出数组中所有重复了两次的元素的更多相关文章
- [LeetCode] 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] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- [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 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
随机推荐
- .htaccess FollowSymlinks影响rewrite功能
Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...
- zabbix之 自定义内存使用率监控报警
配置zabbix当内存剩余不足15%的时候触发报警 zabbix默认的剩余内存报警:Average Lack of available memory on server {HOST.NAME}{T ...
- How to load custom styles at runtime (不会翻译,只有抄了 )
原文 :http://blogs.embarcadero.com/sarinadupont/2013/10/16/how-to-load-custom-styles-at-runtime/ How t ...
- eclipse 安装配置
https://blog.csdn.net/jklinux/article/details/77861450 JAVA环境配置 https://jingyan.baidu.com/article/db ...
- IIS7.5 PHP环境HTTP经常500错误处理方法
IIS使用FastCGI方式配置PHP以后,在编写PHP程序时,经常会出现HTTP 500错误 HTTP 500(Internal Server Error):服务器尝试执行请求时遇到了意外情况.研究 ...
- 算法实践--最小生成树(Prim算法)
前一篇介绍了一种最小生成树的算法--Kruskal算法,本篇介绍另一种Prim算法 算法描述 定义V为端点的集合,A为最小生成树,初始为空.对于每个端点v初始的Key[v]=∞, Parent[v]= ...
- KPPW2.5 漏洞利用--CSRF
kppw2.5 CSRF漏洞复现 漏洞说明 http://192.168.50.157/kppw25/index.php?do=user&view=message&op=send 收件 ...
- vue里面axios使用post
let params = new URLSearchParams(); params.append('action', "login"); params.append('user' ...
- bresenhan算法数学推导
//原帖http://www.doc88.com/p-612602414790.html
- 堆排序(Heapsort)
class Program { static void Main(string[] args) { , , , , , ,}; int size = arr.Length; Heapsort(arr, ...