-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoArrays.java
More file actions
61 lines (53 loc) · 1.52 KB
/
IntersectionOfTwoArrays.java
File metadata and controls
61 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 题目:给定两个数组,求他们相交的元素
* 例如:
* Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
* 解题思路:将一个数组nums1变为集合的形式,结果集也是集合的形式。
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class IntersectionOfTwoArrays {
public static void main(String[] args) {
System.out.println("请输入数组1:");
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String st1[]=s1.split(" ");
int nums1[]=new int[st1.length];
for(int i=0;i<st1.length;i++)
nums1[i]=Integer.parseInt(st1[i]);
System.out.println("请输入数组2: ");
String s2=sc.nextLine();
String st2[]=s2.split(" ");
int nums2[]=new int[st2.length];
for(int i=0;i<st2.length;i++)
nums2[i]=Integer.parseInt(st2[i]);
Solution197 sl=new Solution197();
int result[]=sl.intersection(nums1, nums2);
System.out.println("结果是:");
for(int i=0;i<result.length;i++)
System.out.print(result[i]+" ");
}
}
class Solution197
{
public int[] intersection(int[] nums1,int[] nums2)
{
if(nums1==null || nums2==null) return null;
Set<Integer> set1=new HashSet<Integer>();
Set<Integer> temp=new HashSet<Integer>();
for(int i=0;i<nums1.length;i++)
set1.add(nums1[i]);
for(int j=0;j<nums2.length;j++)
{
if(set1.contains(nums2[j]))
temp.add(nums2[j]);
}
int []result=new int[temp.size()];
int i=0;
for(int num:temp)
result[i++]=num;
return result;
}
}