442. Find All Duplicates in an Array - LeetCode
Question
442. Find All Duplicates in an Array
Solution
题目大意:在数据中找重复两次的数
思路:数组排序,前一个与后一个相同的即为要找的数
Java实现:
public List<Integer> findDuplicates(int[] nums) {
List<Integer> ans = new ArrayList<>();
if (nums.length == 0) return ans;
Arrays.sort(nums);
int last = nums[0];
for (int i=1; i<nums.length; i++) {
if (nums[i] == last) {
ans.add(last);
}
last = nums[i];
}
return ans;
}
Ref
// when find a number i, flip the number at position i-1 to negative.
// if the number at position i-1 is already negative, i is the number that occurs twice.
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
int index = Math.abs(nums[i])-1;
if (nums[index] < 0)
res.add(Math.abs(index+1));
nums[index] = -nums[index];
}
return res;
}
442. Find All Duplicates in an Array - LeetCode的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...
- 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 ...
- 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), ...
- Remove Duplicates from Sorted Array [LeetCode]
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array leetcode java
算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 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 ...
随机推荐
- (stm32学习总结)—对寄存器的理解 _
芯片里面有什么 我们看到的 STM32 芯片是已经封装好的成品,主要由内核和片上外设组成.若与电脑类比,内核与外设就如同电脑上的 CPU 与主板.内存.显卡.硬盘的关系.STM32F103 采用的是 ...
- java中抽象类和抽象方法到底什么关系?请举例说明!
抽象类和抽象方法什么关系?抽象类中可能有抽象方法,也可能没有抽象方法.那位说,就跟没说一样,那抽象类和抽象方法都叫抽象,他们必定有关系,那关系是什么呢?如果一个类中有抽象方法,它必须得是抽象类. An ...
- CCF201812-1小明上学
题目背景 小明是汉东省政法大学附属中学的一名学生,他每天都要骑自行车往返于家和学校.为了能尽可能充足地睡眠,他希望能够预计自己上学所需要的时间.他上学需要经过数段道路,相邻两段道路之间设有至多一盏红绿 ...
- vue和react给我的感受
以下纯属个人使用两个框架的感想和体会: 不知道你们是否有这种感觉~ 我vue和react都用过一段时间,但是vue给我感觉就是经常会忘记语法,需要对照文档才知道怎么写( 难不成是我没喝六个核桃的原因吗 ...
- 利用css3渐变效果实现圆环旋转效果
* { margin: 0; padding: 0; } .stage { width: 200px; height: 130px; margin: 100px auto; position: rel ...
- 自定义user表签发token、自定义认证类、simpleui模块使用
今日内容概要 自定义User表,签发token 自定义认证类 simpleui的使用 多方式登陆接口(后面也写 内容详细 1.自定义User表,签发token # 如果项目中的User表使用auth的 ...
- [资源] 桃宝上十几块买的C#教程网盘
我粗略的看了下觉得挺好的,分享给大家 链接:https://pan.baidu.com/s/1iHuLUXde4_L7NB-Zw9JWNg 提取码:1314
- Java学习day14
可变参数用作方法的形参,方法参数的个数就可变 格式:修饰符 返回值类型 方法名(数据类型...变量名){ } 方法内的形参只能有一个,这里的变量是一个数组 public static <T> ...
- Java学习day21
今天学习了弹窗,除了此前学的按键以外,弹窗也是程序中广泛使用到的一个方面 做了一个简单的弹窗 除了按键以外,有时候我们需要在界面上显示更多的内容,甚至是图片等,这个时候就需要使用到标签 通过标签和Ic ...
- HbuilderX失焦时自动保存编辑器内容
hbuilderX 有一个非常好用的功能:就是自动保存. 而且不需要安装什么插件,只需要在编辑器设置就可以了.接下来我们一起来设置吧: 1.打开我们的hbuilderX编辑器.在最上排选项栏里打开 & ...