We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent afd9429 commit 0162ebfCopy full SHA for 0162ebf
scripts/algorithms/S/Set Mismatch/Set Mismatch.py
@@ -1,22 +1,20 @@
1
-class Solution {
2
-public:
3
- vector<int> findErrorNums(vector<int>& nums)
4
- {
5
- unordered_map<int,int> m;
6
- int p,q;
7
- for(auto &x:nums)
8
9
- m[x]++;
10
- if(m[x]==2)
11
12
- p=x;
13
- break;
14
- }
15
16
- int n=nums.size();
17
- q=(n*(n+1))/2-accumulate(nums.begin(),nums.end(),0)+p;
18
- return {p,q};
19
-
20
21
-};
22
-// if you like the solution plz upvote.
+// Runtime: 150 ms (Top 93.95%) | Memory: 18.00 MB (Top 86.53%)
+
+class Solution:
+ def findErrorNums(self, nums):
+ n = len(nums)
+ v = [0] * (n + 1)
+ missing, duplicate = 0, 0
+ for num in nums:
+ v[num] += 1
+ for i in range(1, len(v)):
+ if v[i] == 2:
+ duplicate = i
+ if v[i] == 0:
+ missing = i
+ return [duplicate, missing]
0 commit comments