[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 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]
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i< nums.length; i++) {
// map nums[i] to index
int index = Math.abs(nums[i]) - 1;
if (nums[index] < 0) {
res.add(index + 1);
}
nums[index] = -nums[index];
}
return res;
}
}
[LC] 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 ...
- 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 ...
- 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), ...
- 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 - 几种不同思路 - (C++)
题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- 【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)时间复杂度内解决这个问题吗? ...
随机推荐
- 图论中TSP问题的LINGO求解与应用
巡回旅行商问题(Traveling Salesman Problem,TSP),也称为货郎担问题.该问题可简单描述为走遍n个城市的最短路.几十年来,出现了很多近似优化算法.如近邻法.贪心算法.最近插入 ...
- 感知机分类(perceptron classification)
概述 在机器学习中,感知机(perceptron)是二分类的线性分类模型,属于监督学习算法.输入为实例的特征向量,输出为实例的类别(取+1和-1). 感知机对应于输入空间中将实例划分为两类的分离超平面 ...
- 用eclipse运行算法第四版的BinarySearch
import java.util.Arrays; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdIn; impo ...
- JavaScript数组打平(4种方法)
let arr = [1, 2, [3, 4, 5, [6, 7, 8], 9], 10, [11, 12]]; flatten1 = arr => arr.flat(Infinity) fla ...
- 12 Spring Data JPA:springDataJpa的运行原理以及基本操作(上)
spring data jpaday1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理 day2:springdatajpa的基本操作 ...
- 基于redis实现锁控制
多数据源 数据源1为锁控制,数据源2自定义,可用于存储. 锁:当出现并发的时候为了保证数据的一致性,不会出现并发问题,假设,用户1修改一条信息,用户2也同时修改,会按照顺序覆盖自修改的值,为了避免这种
- LGOJ1344 追查坏牛奶
Description link 题意概述:给一张图,每条边有边权,求让点 \(1\) 和点 \(n\) 不连通的"最小破坏边权和" 和 "在此基础上的最小破坏边数&qu ...
- Java之多线程窗口卖票问题(Thread)
/** * * 例子:创建三个窗口卖票,总票数为100张.使用继承Thread类的方式 * * 存在线程的安全问题,待解决. * */class Window extends Thread{ priv ...
- 在MyEclipse的Maven环境下,使用mybatis-generator插件自动生成映射文件(接口)及实体类
在数据表比较多的情况下,手动编写sql映射文件和实体类,实在太多过繁琐,而mybatis-generator能自动生成这此东西,减少了重复性的工作量.mybatis-generator的配置容易出现问 ...
- saturates|meteoric|enclose|marooned|predators|Pioneer community|salinization|condenser|embodied
saturates渗透 meteoric蒸汽 enclose包围 Pioneer community 先锋群落 Climax community顶级群落 cumulative积累 Rebound 回弹 ...