鱼C论坛

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

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

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

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

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

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:

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

Each element in the result must be unique.
The result can be in any order.
import java.util.Arrays;
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for(int i :nums1){
            
            if(i < min) min = i;
            if(i > max) max = i;
        }
        
        boolean[] table = new boolean[max - min + 1];
        for(int s : nums1) table[s - min] = true;
        int[] temp = new int[table.length];
        int size = 0;
        for(int j :nums2){
            
            if(j <= max && j >= min && table[j - min]){
                temp[size++] = j;
                table[j - min] = false;
                
            }
        }
        int[] res = new int[size];
        for(int m =0 ; m < size; m++)  res[m] = temp[m];
        
        return res;
    }
}

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-28 13:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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