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的更多相关文章

  1. 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 ...

  2. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  3. 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 ...

  4. 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 ...

  5. 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), ...

  6. leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项

    https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...

  7. 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 ...

  8. 【LeetCode】442. Find All Duplicates in an Array 解题报告(Python& C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 原地变负 日期 题目地址:https://le ...

  9. 442 Find All Duplicates in an Array 数组中重复的数据

    给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? ...

随机推荐

  1. 单机版solr的搭建

    1.1. Solr的环境 Solr是java开发. 需要安装jdk. 安装环境Linux. 需要安装Tomcat. 1.2. 搭建步骤 第一步:把solr 的压缩包上传到Linux系统 第二步:解压s ...

  2. nvm安装教程

    nvm是一个nodejs的版本管理工具 默认安装位置  C:\Users\userName\AppData\Roaming\nvm x   1 C:\Users\userName\AppData\Ro ...

  3. Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). BitcoinJSONRPCClient异常、及其他异常

    1.异常信息 Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path) ...

  4. 69)stack和queue操作

    操作和vector类似  直接看 vector就行了 或者看笔记  C++进阶课程讲义

  5. KMP算法复杂度证明

    引言 KMP算法应该是看了一次又一次,比赛的时候字符串不是我负责,所以学到的东西又还给网上的博客了-- 退役后再翻开看,看到模板,心想这不是\(O(n^2)\)的复杂度吗? 有两个循环也不能看做是\( ...

  6. 3.docker machine 连接 aliyun 远程docker 服务器

    1.在aliyun ecs 创建docker 服务器 docker-machine create -d aliyunecs machine-aliyunecs 2.远程连接 docker 获取客户端 ...

  7. Python - 文件和目录

    # -*- coding: utf-8 -*- import os print(os.name) # 获取操作系统类型 # print(os.uname()) # 获取操作系统的详细信息,Win不支持 ...

  8. 使用node 做静态文件服务器

    # 1. 使用server-static 包 使用node可以非常快速的方法把指定目录共享出去 前提条件:安装了node,附带有npm 要托管的文件目录为 /root/www # 先创建一个目录用来存 ...

  9. Spring 的 IOC 和 AOP 的理解

    Spring 的 IOC 和 AOP 的理解: https://www.jianshu.com/p/bf1adc3b75e6 对Spring的核心(AOP和IOC)的理解(大白话) https://w ...

  10. Netty之内存泄露

    直接内存是IO框架的绝配,但直接内存的分配销毁不易,所以使用内存池能大幅提高性能. 1.为什么要有引用计数器 Netty里四种主力的ByteBuf,其中UnpooledHeapByteBuf底下的by ...