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. A*算法

    A*在游戏设计中有它很典型的用法,是人工智能在游戏中的代表. A*算法在人工智能中是一种典型的启发式搜索算法,为了说清楚 A*算法,我看还是先说说何谓启发式算法. 一.何谓启发式搜索算法: 在说它之前 ...

  2. iOS中富文本NSMutableAttributedString的用法

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"我是富文 ...

  3. Azure AD Connect 手动同步

    我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...

  4. Http client 请求

    public String sendPost(String url, String param) { System.out.println("------------------ 1&quo ...

  5. spring boot整合shiro出现UnavailableSecurityManagerException

    spring boot自带spring security,spring security自然不用说是一个强大的安全框架,但是用惯了shiro,一时半会用不来spring security,所以要在sp ...

  6. wpf ListBox删除

    绑定一个GUID对象 重绑定的数组中移除改对象,重新绑定.

  7. spring batch资料收集

    spring batch官网 Spring Batch在大型企业中的最佳实践 一篇文章全面解析大数据批处理框架Spring Batch Spring Batch系列总括

  8. Jquery实现特效滑动菜单栏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 一个用纯CSS实现的下拉菜单

    用CSS做下拉列表的特效,代码如下: <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> & ...

  10. fineui刷新父页面

    protected override string AfterAddJS() { //TODO 重载这2个函数 可以控制新增和编辑之后执行的JS return AfterSaveJS_ReloadDa ...