[抄题]:

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找出数组中所有重复了两次的元素的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现

    原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...

  4. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  5. [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 ...

  6. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  7. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  8. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

随机推荐

  1. django admin 支持时间更改

    from django.db import models import django.utils.timezone as timezone class Doc(models.Model): add_d ...

  2. WinFormEx

    项目地址 :  https://github.com/kelin-xycs/WinFormEx WinFormEx 一个 用 C# 写的 WinForm 扩展库 , 用于改善 WinForm 的 界面 ...

  3. django 生产环境部署建议

    参考django官方建议 一种优秀的作法是使用前缀/ws/来区分WebSocket连接和普通HTTP连接,以便修改配置后,使Channels更容易部署到生产环境中. 特别是对于大型站点,可以配置像ng ...

  4. Guava 3: 集合Collections

    一.引子 Guava 对JDK集合的拓展,是最成熟且最受欢迎的部分.本文属于Guava的核心,需要仔细看. 二.Guava 集合 2.1 Immutable Collections不可变集合 1.作用 ...

  5. Intellij IDEA 为常用代码添加快捷代码,补全代码

  6. mongodb与mysql命令详细对比

    传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...

  7. 为嵌入式全志V3s荔枝派板卡添加USB MT7601U(小米随身WIFI)驱动

    折腾了了一天终于scan出环境热点了,感觉本来挺简单的事情,网上教程一大把还费了一天的劲,很丧.不过网上教程虽多,但是还还是有些不同之处的,现在特意总结一下 全志V3s荔枝派板卡 添加该驱动的过程. ...

  8. phpwind v9存在命令执行漏洞(登陆后台)

    已知漏洞:https://www.seebug.org/vuldb/ssvid-94465 phpwind v9最新版存在命令执行漏洞(登陆后台) Phpwind_v9.0.2(最新版),phpwin ...

  9. redis学习链接收藏

    1.redis命令大全--官网 2.redis命令大全--中文翻译版 3.源码(注释版):redis3.0 4.程序代码:<redis入门指南(第二版)>第五章 5.最新的redis-st ...

  10. CentOS7.5 安装ssh

    yum -y install openssh-clients 如果出现 Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open. 等错误显示了,原来 ...