返回介绍

solution / 1200-1299 / 1213.Intersection of Three Sorted Arrays / README_EN

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

1213. Intersection of Three Sorted Arrays

中文文档

Description

Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.

 

Example 1:

Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays.

Example 2:

Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764]
Output: []

 

Constraints:

  • 1 <= arr1.length, arr2.length, arr3.length <= 1000
  • 1 <= arr1[i], arr2[i], arr3[i] <= 2000

Solutions

Solution 1: Counting

Traverse the three arrays, count the occurrence of each number, then traverse any one of the arrays. If the count of a number is $3$, add it to the result array.

The time complexity is $O(n)$, and the space complexity is $O(m)$. Here, $n$ and $m$ are the length of the array and the range of numbers in the array, respectively.

class Solution:
  def arraysIntersection(
    self, arr1: List[int], arr2: List[int], arr3: List[int]
  ) -> List[int]:
    cnt = Counter(arr1 + arr2 + arr3)
    return [x for x in arr1 if cnt[x] == 3]
class Solution {
  public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
    List<Integer> ans = new ArrayList<>();
    int[] cnt = new int[2001];
    for (int x : arr1) {
      ++cnt[x];
    }
    for (int x : arr2) {
      ++cnt[x];
    }
    for (int x : arr3) {
      if (++cnt[x] == 3) {
        ans.add(x);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> arraysIntersection(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3) {
    vector<int> ans;
    int cnt[2001]{};
    for (int x : arr1) {
      ++cnt[x];
    }
    for (int x : arr2) {
      ++cnt[x];
    }
    for (int x : arr3) {
      if (++cnt[x] == 3) {
        ans.push_back(x);
      }
    }
    return ans;
  }
};
func arraysIntersection(arr1 []int, arr2 []int, arr3 []int) (ans []int) {
  cnt := [2001]int{}
  for _, x := range arr1 {
    cnt[x]++
  }
  for _, x := range arr2 {
    cnt[x]++
  }
  for _, x := range arr3 {
    cnt[x]++
    if cnt[x] == 3 {
      ans = append(ans, x)
    }
  }
  return
}
class Solution {
  /**
   * @param Integer[] $arr1
   * @param Integer[] $arr2
   * @param Integer[] $arr3
   * @return Integer[]
   */
  function arraysIntersection($arr1, $arr2, $arr3) {
    $rs = [];
    $arr = array_merge($arr1, $arr2, $arr3);
    for ($i = 0; $i < count($arr); $i++) {
      $hashtable[$arr[$i]] += 1;
      if ($hashtable[$arr[$i]] === 3) {
        array_push($rs, $arr[$i]);
      }
    }
    return $rs;
  }
}

Solution 2: Binary Search

Traverse the first array. For each number, use binary search to find this number in the second and third arrays. If found in both, add this number to the result array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.

class Solution:
  def arraysIntersection(
    self, arr1: List[int], arr2: List[int], arr3: List[int]
  ) -> List[int]:
    ans = []
    for x in arr1:
      i = bisect_left(arr2, x)
      j = bisect_left(arr3, x)
      if i < len(arr2) and j < len(arr3) and arr2[i] == x and arr3[j] == x:
        ans.append(x)
    return ans
class Solution {
  public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
    List<Integer> ans = new ArrayList<>();
    for (int x : arr1) {
      int i = Arrays.binarySearch(arr2, x);
      int j = Arrays.binarySearch(arr3, x);
      if (i >= 0 && j >= 0) {
        ans.add(x);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> arraysIntersection(vector<int>& arr1, vector<int>& arr2, vector<int>& arr3) {
    vector<int> ans;
    for (int x : arr1) {
      auto i = lower_bound(arr2.begin(), arr2.end(), x);
      auto j = lower_bound(arr3.begin(), arr3.end(), x);
      if (*i == x && *j == x) {
        ans.push_back(x);
      }
    }
    return ans;
  }
};
func arraysIntersection(arr1 []int, arr2 []int, arr3 []int) (ans []int) {
  for _, x := range arr1 {
    i := sort.SearchInts(arr2, x)
    j := sort.SearchInts(arr3, x)
    if i < len(arr2) && j < len(arr3) && arr2[i] == x && arr3[j] == x {
      ans = append(ans, x)
    }
  }
  return
}

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

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

发布评论

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