返回介绍

solution / 2200-2299 / 2215.Find the Difference of Two Arrays / README_EN

发布于 2024-06-17 01:03:08 字数 7158 浏览 0 评论 0 收藏 0

2215. Find the Difference of Two Arrays

中文文档

Description

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].

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
  • -1000 <= nums1[i], nums2[i] <= 1000

Solutions

Solution 1

class Solution:
  def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
    s1, s2 = set(nums1), set(nums2)
    return [list(s1 - s2), list(s2 - s1)]
class Solution {
  public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
    Set<Integer> s1 = convert(nums1);
    Set<Integer> s2 = convert(nums2);

    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> l1 = new ArrayList<>();
    List<Integer> l2 = new ArrayList<>();
    for (int v : s1) {
      if (!s2.contains(v)) {
        l1.add(v);
      }
    }
    for (int v : s2) {
      if (!s1.contains(v)) {
        l2.add(v);
      }
    }
    ans.add(l1);
    ans.add(l2);
    return ans;
  }

  private Set<Integer> convert(int[] nums) {
    Set<Integer> s = new HashSet<>();
    for (int v : nums) {
      s.add(v);
    }
    return s;
  }
}
class Solution {
public:
  vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
    unordered_set<int> s1(nums1.begin(), nums1.end());
    unordered_set<int> s2(nums2.begin(), nums2.end());
    vector<vector<int>> ans(2);
    for (int v : s1)
      if (!s2.count(v))
        ans[0].push_back(v);
    for (int v : s2)
      if (!s1.count(v))
        ans[1].push_back(v);
    return ans;
  }
};
func findDifference(nums1 []int, nums2 []int) [][]int {
  s1, s2 := make(map[int]bool), make(map[int]bool)
  for _, v := range nums1 {
    s1[v] = true
  }
  for _, v := range nums2 {
    s2[v] = true
  }
  ans := make([][]int, 2)
  for v := range s1 {
    if !s2[v] {
      ans[0] = append(ans[0], v)
    }
  }
  for v := range s2 {
    if !s1[v] {
      ans[1] = append(ans[1], v)
    }
  }
  return ans
}
function findDifference(nums1: number[], nums2: number[]): number[][] {
  return [
    [...new Set<number>(nums1.filter(v => !nums2.includes(v)))],
    [...new Set<number>(nums2.filter(v => !nums1.includes(v)))],
  ];
}
use std::collections::HashSet;
impl Solution {
  pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
    vec![
      nums1
        .iter()
        .filter_map(|&v| if nums2.contains(&v) { None } else { Some(v) })
        .collect::<HashSet<i32>>()
        .into_iter()
        .collect(),
      nums2
        .iter()
        .filter_map(|&v| if nums1.contains(&v) { None } else { Some(v) })
        .collect::<HashSet<i32>>()
        .into_iter()
        .collect()
    ]
  }
}
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[][]}
 */
var findDifference = function (nums1, nums2) {
  let ans1 = new Set(nums1),
    ans2 = new Set(nums2);
  for (let num of nums1) {
    ans2.delete(num);
  }
  for (let num of nums2) {
    ans1.delete(num);
  }
  return [Array.from(ans1), Array.from(ans2)];
};
class Solution {
  /**
   * @param Integer[] $nums1
   * @param Integer[] $nums2
   * @return Integer[][]
   */
  function findDifference($nums1, $nums2) {
    $rs = [[], []];
    $hashtable1 = array_flip(array_unique($nums1));
    $hashtable2 = array_flip(array_unique($nums2));
    for ($m = 0; $m < count($nums1); $m++) {
      if (!isset($hashtable2[$nums1[$m]])) {
        $rs[0][$m] = $nums1[$m];
        $hashtable2[$nums1[$m]] = 1;
      }
    }
    for ($n = 0; $n < count($nums2); $n++) {
      if (!isset($hashtable1[$nums2[$n]])) {
        $rs[1][$n] = $nums2[$n];
        $hashtable1[$nums2[$n]] = 1;
      }
    }
    return $rs;
  }
}

Solution 2

impl Solution {
  pub fn find_difference(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<Vec<i32>> {
    const N: usize = 2001;
    let to_index = |i| (i as usize) + 1000;

    let mut is_in_nums1 = [false; N];
    let mut is_in_nums2 = [false; N];
    let mut res1 = vec![];
    let mut res2 = vec![];
    for &num in nums1.iter() {
      is_in_nums1[to_index(num)] = true;
    }
    for &num in nums2.iter() {
      is_in_nums2[to_index(num)] = true;
      if !is_in_nums1[to_index(num)] {
        res2.push(num);
        is_in_nums1[to_index(num)] = true;
      }
    }
    for &num in nums1.iter() {
      if !is_in_nums2[to_index(num)] {
        res1.push(num);
        is_in_nums2[to_index(num)] = true;
      }
    }
    vec![res1, res2]
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文