Find the Difference of Two Arrays
给两个数组, 求两个list, 第一个list是第一个数组中不含第二个数组的数字, 第二个list是第二个数字中不含第一个数组的数字.
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 |
class Solution { public List<List<Integer>> findDifference(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet<>(); Set<Integer> set2 = new HashSet<>(); Set<Integer> set3 = new HashSet<>(); Set<Integer> set4 = new HashSet<>(); for(int n : nums1){ set1.add(n); } for(int n : nums2){ set2.add(n); } for(int n : nums1){ set2.remove(n); } for(int n : nums2){ set1.remove(n); } List<Integer> l1 = new ArrayList<>(set1); List<Integer> l2 = new ArrayList<>(set2); List<List<Integer>> res = new ArrayList<>(); res.add(l1); res.add(l2); return res; } } |