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]
public class Solution {
//使用index和负数来表示这个位置被访问过。 public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
for (int i = ; i < nums.length; ++i) {
int index = Math.abs(nums[i]) - ;
if (nums[index] < ) {
res.add(Math.abs(index + ));
}
nums[index] = -nums[index];
}
return res;
}
}
Find All Duplicates in an Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
随机推荐
- LUA 利用#遍历表的问题
tb ={ } t = { "hello", , , 'w', , tb } --~ 1 hello --~ 2 1 --~ 3 2 --~ 4 w --~ 5 4 --~ 6 t ...
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- CBOW and Skip-gram model
转自:https://iksinc.wordpress.com/tag/continuous-bag-of-words-cbow/ 清晰易懂. Vector space model is well k ...
- jeecg单步调试
自己没本事写开发平台,用别人的又各种担心,想学着别人弄个单步调试,老是出现这个"source not found"-- 百度各种方法都搞不定,担心是自己安装错了?这个jeecg本身 ...
- C#-WebForm-WebForm开发基础
1.C/S 客户端应用程序 WinForm WPF 平级 数据是存放在其他的电脑上或服务器上 需要从服务器上下载相应的数据,在本地电脑上的客户端里进行加工 数据的加工是在用户的电脑上执行的,会对用户的 ...
- 光盘刻录 CD刻录软件 Ashampoo Burning Studio特别版 刻录CD就这么简单
著名的刻录软件Nero,其近上百M体积实在太大,而且安装之后的文件体积也有上G多.这么大的体积安装使用都不方便,好在现在很多都做得很不错,比如阿香婆的光盘刻录软件Ashampoo® Burning S ...
- download ncRNA sequences form NCBI
#!/bin/bash usage() { echo;echo "Usage: ./`basename $0` [gi number list] [number of cpu]"; ...
- elk系列4之kibana图形化操作
preface 我们都搭建了ELK系统,且日志也能够正常收集的时候,那么就配置下kibana.我们可以通过kibana配置柱状图,趋势图,统计图,圆饼图等等各类图.下面就拿配置统计图和柱状图为例,结合 ...
- [创业中, 寻求合作] 业务方向:车联网智能终端;APP蓝牙控制汽车;APP网络远程控制汽车 (联系电话:18503086002)
擅长领域 手机APP蓝牙控制汽车方案 手机APP网络远程控制汽车方案 手机APP与汽车车机的文件极速传输技术 车载OBD终端 (后装) 智能TBOX终端,Base on Linux,使用车规级硬件加密 ...
- struts2+hibernate 项目实战:图书管理系统
经典项目,练手必备. 图书管理系统 需求分析(大致,并不专业):1.需要有用户管理: 1.1 用户注册: 1.2 用户登录: 1.3 用户信息修改: 1.4 用户修改密码: 2.需要有书本管理: 2. ...