forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2691 from AHTHneeuhl/2215
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2. | ||
// Space Complexity: O(m), where m is the length of the resulting difference vectors. | ||
|
||
class Solution | ||
{ | ||
public: | ||
vector<vector<int>> findDifference(vector<int> &nums1, vector<int> &nums2) | ||
{ | ||
unordered_set<int> nums1Set(nums1.begin(), nums1.end()); | ||
unordered_set<int> nums2Set(nums2.begin(), nums2.end()); | ||
|
||
vector<int> lst1; | ||
vector<int> lst2; | ||
|
||
for (const auto &num : nums1Set) | ||
{ | ||
if (nums2Set.find(num) == nums2Set.end()) | ||
{ | ||
lst1.push_back(num); | ||
} | ||
} | ||
|
||
for (const auto &num : nums2Set) | ||
{ | ||
if (nums1Set.find(num) == nums1Set.end()) | ||
{ | ||
lst2.push_back(num); | ||
} | ||
} | ||
|
||
return {lst1, lst2}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @param {number[]} nums1 | ||
* @param {number[]} nums2 | ||
* @return {number[][]} | ||
*/ | ||
|
||
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2. | ||
// Space Complexity: O(m), where m is the length of the resulting difference lists. | ||
|
||
var findDifference = function (nums1, nums2) { | ||
const nums1Set = new Set(nums1); | ||
const nums2Set = new Set(nums2); | ||
|
||
const lst1 = Array.from(nums1Set).filter((num) => !nums2Set.has(num)); | ||
const lst2 = Array.from(nums2Set).filter((num) => !nums1Set.has(num)); | ||
|
||
return [lst1, lst2]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Time Complexity: O(n), where n is the maximum length between nums1 and nums2. | ||
# Space Complexity: O(m), where m is the length of the resulting difference lists. | ||
|
||
from typing import List # ignore this, just for typing | ||
|
||
|
||
class Solution: | ||
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: | ||
nums1_set = set(nums1) | ||
nums2_set = set(nums2) | ||
lst1 = [num for num in nums1_set if num not in nums2_set] | ||
lst2 = [num for num in nums2_set if num not in nums1_set] | ||
return [lst1, lst2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Time Complexity: O(n), where n is the maximum length between nums1 and nums2. | ||
// Space Complexity: O(m), where m is the length of the resulting difference lists. | ||
|
||
function findDifference(nums1: number[], nums2: number[]): number[][] { | ||
const nums1Set: Set<number> = new Set(nums1); | ||
const nums2Set: Set<number> = new Set(nums2); | ||
|
||
const lst1: number[] = Array.from(nums1Set).filter( | ||
(num: number) => !nums2Set.has(num) | ||
); | ||
const lst2: number[] = Array.from(nums2Set).filter( | ||
(num: number) => !nums1Set.has(num) | ||
); | ||
|
||
return [lst1, lst2]; | ||
} |