首页 > 移动平台 > 详细

LeetCode "448. Find All Numbers Disappeared in an Array"

时间:2017-01-06 09:32:48      阅读:182      评论:0      收藏:0      [点我收藏+]

My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra mem is allowed, we can simply use ‘sign‘ on each index slot to mark the same info.. it is a common technique.

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        vector<int> ret;
        int n = nums.size();
        if(!n) return ret;
        
        // Pass 1 - use sign to mark existence.
        for(int i = 0; i < n; i ++)
        {
            int inx = abs(nums[i]) - 1;
            nums[inx] = -abs(nums[inx]);
        }   
        
        // Pass 1 - get all positive
        for(int i = 0; i < n; i ++)
            if(nums[i] > 0)
                ret.push_back(i + 1);
        
        return ret;
    }
};

LeetCode "448. Find All Numbers Disappeared in an Array"

原文:http://www.cnblogs.com/tonix/p/6254902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!