442. Find All Duplicates in an Array
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的更多相关文章
- 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 ...
- 442. Find All Duplicates in an Array - LeetCode
Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...
- 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 ...
- leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项
https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...
- 442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...
随机推荐
- Visual Stdio 无法直接启动带有“类库输出类型”的项目若要调试此项目,请在此解决方案中添加一个引用库项目的可执行项目。将这个可执行项目设置为启动项目!
j解决方法:项目-属性-应用程序-输出类型-Windows应用程序
- C#设计模式(3)——工厂方法模式
一.概念:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 二.代码实现 namespace 设计模式之工厂方法模式 { /// <summary&g ...
- shell及脚本3——正则表达式
一.正则表达式 1.1. 什么是正则表达式 正则表达式是处理字符串的方法,以行为单位,通过一些特殊符号的辅助,让用户可以轻易进行查找.删除.替换某特定字符串的操作. 1.2. 正则表达式与通配符的区别 ...
- Appium简单测试用例
工程目录如下: 封装初始化androiddriver 方法 import org.openqa.selenium.remote.DesiredCapabilities; import java.io. ...
- PRINCE2特征(三)
提到不确定性,可能很多从事项目管理相关工作的人都会感同身受,一系列临时性问题,比如:变更.延期.调整.计划赶不上变化.团队调整等的出现,都是项目中再正常不过的内容.但正常不等于合理,我们还是要去思考一 ...
- 【Android群英传】学习笔记(二)
在本篇笔记中,小编将记录关于Android Studio使用过程的小技巧 在下面文章为Android Studio 简称As ① 使用Android Studio第一次创建项目等待时间很长? 原因: ...
- ZKW线段树
简介 zkw线段树虽然是线段树的另一种写法,但是本质上已经和普通的递归版线段树不一样了,是一种介于树状数组和线段树中间的存在,一些功能上的实现比树状数组多,而且比线段树好写且常数小. 普通线段树采用从 ...
- ueditor工具栏新增按钮教程
我做了一个人博客网站想要一段文字高亮显示,大概是这样: 但是ueditor上面的代码语言是一大块的<pre></pre>标签,觉得不合适,就在网上搜索相关文章,自己结合着实现了 ...
- cookie---session
//以下文字摘自慕课网教程..... 设置cookie PHP设置Cookie最常用的方法就是使用setcookie函数,setcookie具有7个可选参数,我们常用到的为前5个: name( Coo ...
- JavaScript 实现5秒倒计时,接着跳转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...