442. Find All Duplicates in an Array

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]


refer:

点击打开链接 点击打开链接2

class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        vector<int> res;
        int len = nums.size();
        if( len<=1 )
        return res;
        //method 1.交换位置法
        //思想:遍历nums,每次按元素值作为坐标索引映射到其对应位置,若俩元素值相同,则会重复映射至同一位置,此时,类似于二者抵消,将该值赋值为0;最终目标,重复出现导致位置冲突的元素赋值为0,得到仅出现一次的元素按值映射重排列的num
        //149 ms,beat 75%
        for( int i=0; i<len; ++i ){
            while( (nums[i]>0)&&(nums[i]!=i+1) ){  //未标识为结果、且待交换对象不是自身
                if( nums[i]==nums[nums[i]-1] ){
                    res.push_back(nums[i]);
                    nums[i] = 0;
                    //nums[nums[i]-1] = 0;
                }else{
                    swap(nums[i],nums[nums[i]-1]);
                }
            }
        }
/*
        //metiod 2. 负值标记法
        //思想:按照值的绝对值映射到相应位置,若第一次出现,将该位置自己的值标识为负值,第二次出现,则会发现该位置的值已经为负值,加入res
        for( int i=0; i<len; ++i ){
            //int index = abs(nums[i])-1;             //156ms,beats 58%
            int index = (nums[i]>0) ? (nums[i]-1) : (-nums[i]-1);            //175ms,beats 45%
            if( nums[index]<0 ){
                res.push_back(index+1);
            }else{
                nums[index] = -nums[index];
            }
        }
*/        return res;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值