[抄题]:

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. CVE-2017-11882 POC 全版本通杀

    POC https://github.com/embedi/CVE-2017-11882

  2. python 的 format 函数

    python的格式化字符串方法之一------------format 函数 它通过{}和:来代替%. 数字 格式 输出 描述 3.1415926 {:.2f} 3.14 保留小数点后两位 3.141 ...

  3. IETF和W3C的区别

    国际互联网协会ISOC和万维网联盟W3C是互联网领域内两大国际协会组织,ISOC旗下IETF机构与W3C是互联网行业内两大标准组织.不可否认「IETF和W3C都是优秀的组织,它们从事着一种使事情按程序 ...

  4. day052 django第三天 url和视图

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...

  5. windows下matplotlib编译安装备忘

    windows下,codeblocks,mingw安装matplotlib. python下一些源码的编译安装,备忘. matplotlib官网编译好的版本只支持到3.3.我不慎刚下了python3. ...

  6. G2( bizCharts ) React 绘制混合图例

    G2( bizCharts ) React 绘制混合图例, // data-set 可以按需引入,除此之外不要引入别的包 import React from 'react'; import { Cha ...

  7. php 7 event 安装

    有效安排I/O,时间和信号的扩展 使用可用于特定平台的最佳I/O通知机制的事件,是PHP基础设施的libevent端口. 下载地址:http://pecl.php.net/package/event ...

  8. iOS上传本地代码到git

    1.顾名思义,首先你得注册一个github账户 这个我就不细说了. 2.然后你得创建一个 repository  步骤见下图 3.相当于创建成功 会跳到下图界面 4.一看就很清楚了 create a ...

  9. TensorFlow 安装报错的解决办法(安装1.5版本)

    1.安装Anaconda 百度下载windows版本,一路点下一步,安装好了Anaconda,自带python3.6.6. 2.安装TensorFlow (1)打开Anaconda Prompt,输入 ...

  10. Ajax提交from表单

    一,使用Ajax提交form表单到后台传参问题 1,首先,定义一个form: <form class="form-horizontal" role="form&qu ...