Set Mismatch

2018/1/18 posted in  leetcode

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int len = nums.size();
        vector<int> res;
        vector<int> a(len+1);   //因为数组从0开始,所以除去0,在后面加上1位

        for (int i = 0; i<len; i++) {
            if (a[nums[i]]==0) {
                a[nums[i]]+=1;
            }else{
                res.push_back(nums[i]);
            }
        }

        for (int j = 1; j<=len; j++) {
            if (a[j]==0) {
                res.push_back(j);
            }
        }

        return res;
    }
};