鱼C论坛

 找回密码
 立即注册
查看: 2325|回复: 0

[学习笔记] leetcode 349. Intersection of Two Arrays

[复制链接]
发表于 2019-9-7 02:58:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given two arrays, write a function to compute their intersection.

  2. Example 1:

  3. Input: nums1 = [1,2,2,1], nums2 = [2,2]
  4. Output: [2]
  5. Example 2:

  6. Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
  7. Output: [9,4]
  8. Note:

  9. Each element in the result must be unique.
  10. The result can be in any order.
复制代码

  1. import java.util.Arrays;
  2. class Solution {
  3.     public int[] intersection(int[] nums1, int[] nums2) {
  4.         int min = Integer.MAX_VALUE;
  5.         int max = Integer.MIN_VALUE;
  6.         for(int i :nums1){
  7.             
  8.             if(i < min) min = i;
  9.             if(i > max) max = i;
  10.         }
  11.         
  12.         boolean[] table = new boolean[max - min + 1];
  13.         for(int s : nums1) table[s - min] = true;
  14.         int[] temp = new int[table.length];
  15.         int size = 0;
  16.         for(int j :nums2){
  17.             
  18.             if(j <= max && j >= min && table[j - min]){
  19.                 temp[size++] = j;
  20.                 table[j - min] = false;
  21.                
  22.             }
  23.         }
  24.         int[] res = new int[size];
  25.         for(int m =0 ; m < size; m++)  res[m] = temp[m];
  26.         
  27.         return res;
  28.     }
  29. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 17:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表