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 ...
随机推荐
- BEAM188和LINK180简单实例
简介 一开始想做一个绳索单元悬挂重物的仿真,其实想法很简单,但是在实现过程中却出现了很大的问题,纠结了很久,初步归结为:绳索单元在垂直其单元方向上受力,会导致其产生很大的变形,从而导致其不收敛.因此专 ...
- POJ1738 An old Stone Game
题意 Language:Default An old Stone Game Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 439 ...
- python3中的编码
python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...
- oidc User.Identity.Name 为空解决方法
public override Task TicketReceived(TicketReceivedContext context) { var result = base.TicketReceive ...
- sql server紧急状态下登录脚本
--打开xp_cmdshell功能 EXEC [sys].[sp_configure] @configname = 'xp_cmdshell', -- varchar(35) @configv ...
- time&datetime
关于time模块的代码部分 1 #_*_coding:utf-8_*_ 2 __author__ = 'Alex Li' 3 4 import time 5 6 7 # print(time.cloc ...
- Python【每日一问】01
问:深拷贝.浅拷贝.直接赋值的区别是什么?并举例说明 1.区别 (1)直接赋值:对象的引用 (2)浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象 (3)深拷贝(deepcopy): cop ...
- [UE4]Background Blur,背景模糊
一.Blur Strength:模糊强度 二.背景是模糊的,在Background Blur前面的控件二不会模糊. 三.可以调整顺序,让按钮也模糊.然后按钮被模糊了,但是按钮还可以被点击的,Backg ...
- JAVA 异常类型结构分析
JAVA 异常类型结构分析 Throwable 是所有异常类型的基类,Throwable 下一层分为两个分支,Error 和 Exception. Error 和 Exception Error Er ...
- [图文教程]VS2017搭建opencv & C++ 开发环境
首先从官网下载OpenCV最新版本 截至我写这文章,4.0已经发布预览版了,不过在这是没有的,只能用3.4.2: https://opencv.org/releases.html 一:安装 安装过程不 ...