将数组内的数与数组下标建立联系,若某数在数组中出现过,则将下标为该数减1的数变为负数,最后统计该数组正数所对应的下标值。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> disapperNums;
int m;
for (int i = 0; i < nums.size(); ++i) {
m = abs(nums[i]) - 1;
nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
}
for (int i = 0; i < nums.size(); ++i ) {
if (nums[i] > 0) {
disapperNums.push_back(i + 1);
}
}
return disapperNums;
}
};