題目會給定我們兩個整數陣列作為輸入nums1, nums2,要求我們找出兩個陣列的差異值。
找出在nums1但是不在nums2的元素,以陣列的形式放在answer[0]輸出。
找出在nums2但是不在nums1的元素,以陣列的形式放在answer[1]輸出。
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].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
Output: [[3],[]]
Explanation:
For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
1 <= nums1.length, nums2.length <= 1000
陣列nums1 和 nums2的長度都介於1 ~ 1000之間
-1000 <= nums1[i], nums2[i] <= 1000
每個陣列元素都介於-1000 ~ 1000之間。
看到存在性的檢查,聯想到集合set操作。
題目的要求是
找出在nums1但是不在nums2的元素,以陣列的形式放在answer[0]輸出。
找出在nums2但是不在nums1的元素,以陣列的形式放在answer[1]輸出。
相當於對應的集合操作
set( nums1 ) - set( nums1 ) 交集 set( nums2 )
set( nums2 ) - set( nums1 ) 交集 set( nums2 )