接下來要進到medium題了!!!
You are given an integer array nums representing the data status of this set after the error.
Find the number that occurs twice and the number that is missing and return them in the form of an array.
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int duplicate = -1, missing = -1;
int n = nums.size();
vector<int> count(n + 1, 0);
for (int num : nums) {
count[num]++;
}
for (int i = 1; i <= n; i++) {
if (count[i] == 2) {
duplicate = i;
} else if (count[i] == 0) {
missing = i;
}
}
return {duplicate, missing};
}
};