forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-mismatch.cpp
47 lines (44 loc) · 1.2 KB
/
set-mismatch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int x_xor_y = 0;
for (int i = 0; i < nums.size(); ++i) {
x_xor_y ^= nums[i] ^ (i + 1);
}
int bit = x_xor_y & ~(x_xor_y - 1);
vector<int> result(2);
for (int i = 0; i < nums.size(); ++i) {
result[static_cast<bool>(nums[i] & bit)] ^= nums[i];
result[static_cast<bool>((i + 1) & bit)] ^= i + 1;
}
if (find(nums.begin(), nums.end(), result[0]) == nums.end()) {
swap(result[0], result[1]);
}
return result;
}
};
// Time: O(n)
// Space: O(1)
class Solution2 {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> result(2);
for (const auto& i : nums) {
if (nums[abs(i) - 1] < 0) { // twice
result[0] = abs(i);
} else {
nums[abs(i) - 1] *= -1;
}
}
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > 0) { // missing
result[1] = i + 1;
} else {
nums[i] *= -1;
}
}
return result;
}
};