https://leetcode.com/problems/find-all-duplicates-in-an-array/

一列数,1 ≤ a[i] ≤ n (n = size of array),有的出现一次有的出现两次,输出出现两次的数。

Example:

Input:
[4,3,2,7,8,2,3,1] Output:
[2,3] 耿直的你大概会写出如下代码
class Solution(object):
def findDuplicates(self, nums):
myset=set()
for x in nums:
if nums.count(x)==2:
myset.add(x)
return list(myset)

这就是engineering和science的区别,在工程中可能要统计2,也有可能以后需求要变会统计100,这时候这个代码改动就少

但是这可是LeetCode,在遍历中用count()妥妥会TLE,题目也说了Could you do it without extra space and in O(n) runtime?

这时候你就需要用到各种奇技淫巧了

注意到:

1、数组内元素只会出现一次或两次,没有其他的情况。

2、所有元素都是正整数,且大于等于1

然后你突开脑洞,把数组遍历,将nums[x-1]的数翻面变负,如果在遍历中判断的时候发现这个数已经是负的了,说明出现了两次

class Solution(object):
def findDuplicates(self, nums):
res = []
for x in nums:
if nums[abs(x) - 1] < 0:
res.append(abs(x))
else:
nums[abs(x) - 1] *= -1
return res

什么?觉得有点看不懂?来个简单易懂的,也能过

class Solution(object):
def findDuplicates(self, nums):
res = []
for x in nums:
nums[abs(x) - 1] *= -1
for x in nums:
if nums[abs(x) - 1] > 0:
res.append(abs(x))
return list(set(res))

442. Find All Duplicates in an Array的更多相关文章

  1. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  2. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  3. LeetCode 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 ...

  4. leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项

    https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...

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

  6. LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)

    题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...

  7. [LC] 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 ...

  8. 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...

  9. 442 Find All Duplicates in an Array 数组中重复的数据

    给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...

随机推荐

  1. Java学习笔记(一)

    纯属个人学习笔记,有什么不足之处大家留言,谢谢 Java程序打包与JAR运行方法 在Eclipse的"包资源管理器"视图中找到要打包成JAR文件的项目.在项目名称上单击鼠标右键,在 ...

  2. 关于delphi exit 继承

    父类窗体frm 继承下来一个子类 form2 父类按钮代码 procedure Tfrm.Button1Click(Sender: TObject); begin ShowMessage('); Ex ...

  3. java编码

    /**     * @Comments:default:ISO-8859-1 -> UTF-8 乱码则转码     * @param: str(乱码字符),coding(default:ISO- ...

  4. 通过WebStorm上传代码至github

    首先需要注册github帐号,具体方法自行百度/谷歌. 打开WebStorm,我用的是2016.2.4版本(如果你有edu邮箱的话,可以免费使用一年,不只是Webstorm,JetBrains全家桶都 ...

  5. ajax-登陆+验证码

    登陆用户名和密码判断+验证码验证 省略dao层和service层 1.生成验证码的number.jsp <%@ page contentType="image/jpeg" l ...

  6. struts2-上传文件

    package web; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; i ...

  7. POJ1742Coins(并不理解是什么意思)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32309   Accepted: 10986 Descripti ...

  8. 数据结构之平衡查找树(AVL)

    AVL树的旋转操作 图解 最详细 各大教课书上讲的都是左旋与右旋,其实这样很容易理解错误,我们换一种叫法.我们称呼左旋为:逆进针旋转.我们称呼右旋为:顺进针旋转.

  9. html5中画布和SVG的比较

    SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...

  10. 【转载】在IT界取得成功应该知道的10件事

     在IT界取得成功应该知道的10件事 2011-08-11 13:31:30 分类: 项目管理 导读:前面大多数文章都是Jack Wallen写的,这是他的新作,看来要成为NB程序员还要不停的自我总结 ...