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 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找出数组中所有重复了两次的元素的更多相关文章
- [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 ...
- [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 ...
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [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 ...
- lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 样例 ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
随机推荐
- PythonStudy——三种字符串 Three strings
# 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...
- linux(centos6.9)安装步骤
http://blog.csdn.net/u012453843/article/details/52819756 https://mirrors.aliyun.com/ 下载想要的版本 2选择新建虚拟 ...
- 阅读 Device Driver Programmer Guide 笔记
阅读 Device Driver Programmer Guide 笔记 xilinx驱动命名规则 以X开头 源文件命名规则 以x打头 底层头文件与高级头文件 重点来了,关于指针的使用 其中 XDev ...
- Java多态(非常重要)
多态(多种形态)的定义 同一消息对不同类的对象做出的不同响应 种类 在程序设计中一般说多态都是运行时多态 多态施行的条件: 1满足继承关系 2父类引用指向子类对象(向上转型) 向上转型 向下转型(子类 ...
- 【剑指offer】广度优先遍历二叉树
问题:从上往下打印出二叉树的每个节点,同层节点从左至右打印. *思路:先用队列存放树的根结点.每次出队一个结点,将结点非空的左右孩子分别入队.重复此过程,直到队列为空. import java.uti ...
- C/C++中的volatile简单描述
首先引入一篇博客: 1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 ...
- [UE4]Retainer Box
把子元素的内容渲染到一个Render Target上去,然后放把它放置到到屏幕上去. Retainer Box的作用: 1.控制UI更新频率 2.把渲染后的UI当成Texture,放入材质中,加工后, ...
- Sql server 编写99乘法表
Sql 组织编写语句 declare @one int,@tow int,@str varchar(100),@num intselect @one=1while(@one<=9)beginse ...
- atnodes命令使用方法
一条命令可以同时执行多台机器,结果会输出列表. atnodes -L 'grep -c "查询订单列表,userId=bing.wang03" /home/w/www/order- ...
- laravel代码生成器的记录
在使用laravel5.5时,关于代码生成器还有很多的功能不明白 是么回事,在此先记录下来过程. 1.composer reqiure summerblue/generator --dev php a ...