leetcode442

来源:互联网 发布:hex 编辑器 c语言 编辑:程序博客网 时间:2024/05/02 04:39

1、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:    vector<int> findDuplicates(vector<int>& nums) {        unordered_map<int,int> num;        vector<int> result;        for(auto i:nums)            ++num[i];        for(auto i=num.begin();i!=num.end();i++){            if((*i).second>1)                result.push_back((*i).first);        }        return result;    }};

感觉比较简单粗暴QvQ

0 0
原创粉丝点击