|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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;
- }
- }
复制代码 |
|