Question

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]

Solution

哈希,顺便复习一下STL中set容器用法。

Code

class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
set<int> table;
vector<int> res;
for (int i : nums) {
if (table.find(i) == table.end())
table.insert(i);
else
res.push_back(i);
}
return res;
}
};

LeetCode——Find All Duplicates in an Array的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  3. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

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

  5. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  10. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. Java/android 里ClassName.this和this的使用

    如果在内部类里面用this就是指这个内部类的实例,而如果用OuterClassName.this就是它外面的那个类的实例 ClassName.this这个用法多用于在nested class(内部类) ...

  2. 多线程入门-第四章-线程的调度与控制之sleep

    /* sleep,阻塞当前线程,腾出CPU,让给其他线程 单位是毫秒 静态方法 */ public class ThreadTest04 { public static void main(Strin ...

  3. paintschainer项目

    github:https://github.com/pfnet/PaintsChainer tensorflow实现:https://github.com/mizti/tensor_paint 在线测 ...

  4. 2018 又一个假期 over 了

    五一假期三天  在家窝三天  早上起床仍感觉没休息好  其实确实没休息好  假期的时间总不愿意拿来补觉用.周末准备去体检,真心恐惧医院的味道,硬着头皮还得上啊.

  5. 禁止Centos系统You have new mail in /var/spool/mail/root提示

    禁止Centos系统You have new mail in /var/spool/mail/root提示 https://blog.csdn.net/oyym_mv/article/details/ ...

  6. Windows服务的安装、卸载

    创建一个Windows服务 http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 安装服务 使用FramWork框架自带的Ins ...

  7. 在firefox安装Selenium IDE

    1.打开地址:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/: 2.点击Add to Firefox: 3.打开Firefox ...

  8. Linux系统——shell脚本

    shell脚本编程 作用:通过命令行解析的方式,自动执行设定好的程序或命令代码.(若将脚本挂到定时任务中,就会自动在非工作时间里自动触发执行程序) Shell脚本文件以“.sh”结尾 规范的Shell ...

  9. XDU 1031

    #include<stdio.h> #define maxn 1005 int c[maxn][maxn]; int gcd(int a,int b){ ?a:gcd(b,a%b); } ...

  10. HTTPS原理解析-转

    这篇文章关于Https的讲解真的是太透彻了,转过来备忘. 来源:腾讯bugly 另附两个SSL/TLS的交互详解:一.二 基于此文章的学习总结:下一篇文章 1.HTTPS 基础 HTTPS(Secur ...