博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
442. Find All Duplicates in an Array
阅读量:4338 次
发布时间:2019-06-07

本文共 619 字,大约阅读时间需要 2 分钟。

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]

解题思路:

用unordered_map扫描一遍。

 

class Solution {

public:
vector<int> findDuplicates(vector<int>& nums) {
unordered_map<int,int> store;
vector<int> duplicate;
for(int i=0;i<nums.size();i++){
if(store.count(nums[i]) == 1) {
duplicate.push_back(nums[i]);
}
else{
store[nums[i]]++;
}
}
return duplicate;
}
};

转载于:https://www.cnblogs.com/liangyc/p/8783807.html

你可能感兴趣的文章
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
新部署到服务器 报 The requested URL /home/profession was not found on this server. 错误
查看>>
hadoop从非HA转到NAMENODE HA时需要注意的一个问题
查看>>
KnockoutJs学习笔记(十一)
查看>>