-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path645. Set Mismatch.cpp
99 lines (79 loc) · 2.52 KB
/
645. 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// -----Approach 1: using set ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/set-mismatch/
Time: 84 ms (Beats 38.18%), Space: 33.1 MB (Beats 10.79%)
*/
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int n=nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0);
vector<int> ans;
set<int> s;
for(auto &i: nums) s.insert(i);
int sumSet = accumulate(s.begin(), s.end(), 0);
ans.push_back(sum-sumSet);
ans.push_back( n*(n+1)/2 - sumSet );
return ans;
}
};
// -----Approach 2: using map ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/set-mismatch/
Time: 179 ms (Beats 7.27%), Space: 32.9 MB (Beats 15.76%)
*/
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
int n=nums.size();
int sum=(n*(n+1))/2;
vector<int> ans;
map<int,int> mpp;
for(auto i: nums) mpp[i]++;
for(auto it: mpp){
if(it.second > 1) ans.push_back(it.first);
sum-=it.first;
}
ans.push_back(sum);
return ans;
}
};
// -----Approach 3: using XOR ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/set-mismatch/
Time: 2 ms (Beats 7.27%), Space: 43.9 MB (Beats 93.24%)
*/
class Solution {
public int[] findErrorNums(int[] nums) {
int n = nums.length;
int[] count = new int[n];
int[] ans = {0,0};
for(int i = 0; i < n; i++) {
ans[1] ^= (i+1) ^ nums[i];
if (++count[nums[i]-1] == 2)
ans[0] = nums[i];
}
ans[1] ^= ans[0];
return ans;
}
}
// -----Approach 4: using Maths ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/set-mismatch/
Time: 59 ms (Beats 56.8%), Space: 21.4 MB (Beats 88.14%)
*/
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
long diff = 0;
long square_diff = 0;
for (int i = 0; i < size(nums); ++i) {
diff += i + 1 - nums[i];
square_diff += (i + 1) * (i + 1) - nums[i] * nums[i];
}
long s = square_diff / diff;
const int dup = (s - diff) / 2;
const int miss = (s + diff) / 2;
return {dup, miss};
}
};