Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details

describe

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
Copy the code

Note:

1 <= nums1.length, nums2.length <= 1000
-1000 <= nums1[i], nums2[i] <= 1000
Copy the code

parsing

Given two integer arrays with index 0, nums1 and nums2, return a list of size 2. The integers in the list can be returned in any order.

  • Answer [0] is a list of all the different integers in NUMs1 that do not exist in NUMs2
  • Answer [1] is a list of all the different integers in NUMs2 that do not exist in NUMs1

The length of nums1 and nums2 is up to 1000, so we need to solve the problem by violence. First we iterate through the elements in nums1, find the elements that do not exist in nums2, and then put them into a list a. Then iterate through the elements in NUMs2, find the elements that do not exist in NUMs1, and then put them into a list B. Finally, join a and B into a result list and return.

The time complexity is O(N), and the space complexity is O(N).

answer

class Solution(object):
    def findDifference(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[List[int]]
        """
        a = []
        for c in nums1:
            if c not in nums2 and c not in a:
                a.append(c)
        b = []
        for c in nums2:
            if c not in nums1 and c not in b:
                b.append(c)
        return [a,b]
                
        	      
		
Copy the code

The results

202/202 Test cases passed. Status: Accepted Runtime: 930 MS Memory Usage: 13.6 MBCopy the code

parsing

Of course, the above solution is purely for the brainless quick solution of the violent solution, in fact, the meaning of this problem is quite clear, the most concise method is certainly to use the set of difference set.

The time complexity is O(N), and the space complexity is O(N).

By comparing the time consuming of the two solutions, we find that this solution saves more time.

answer

class Solution(object):
    def findDifference(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[List[int]]
        """
        s1 = set(nums1)
        s2 = set(nums2)
        a = list(s1-s2)
        b = list(s2-s1)
        return [a,b]
Copy the code

The results

202/202 Test cases passed. Status: Accepted Runtime: 193 MS Memory Usage: 13.7 MBCopy the code

The original link

Leetcode.com/contest/wee…

Your support is my biggest motivation