Set equality problem
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
Given two sets S and T, try to design a Monte Carlo algorithm to determine whether S and T are equal.
Design a Las Vegas algorithm to determine whether given sets S and T are equal.
The first line of Input
input data has a positive integer n (n≤10000), which represents the size of the set. The next 2 lines, each with n positive integers, represent the elements in the sets S and T, respectively.
Output
will compute the conclusion output. If the sets S and T are equal, output YES, otherwise output NO.
Sample Input
3
2 3 7
7 2 3
Sample Output
YES
Hint
Source
import java.util.*;
import java.util.Scanner;
public class Main22 {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
List<Integer> l1 = new LinkedList<Integer>();
List<Integer> l2 = new LinkedList<Integer>();
for(int i = 0;i < n;i++) {
l1.add(input.nextInt());
}
for(int i = 0;i < n;i++) {
l2.add(input.nextInt());
}
if(l1.containsAll(l2)) { //判断l1中是否包含l2中所有的元素,与顺序无关
System.out.println("YES");
}
else {
System.out.println("NO");
}
/*Iterator<Integer> it1 = l1.iterator(); 这是最开始自己写的,对list的类方法并不是很清楚,比较复杂,后来看到大佬的博客才知道可以用containsall方法
//int flag = 0;
/*while(it1.hasNext()) {
flag = 0;
int x = it1.next();
Iterator<Integer> it2 = l2.iterator();
while(it2.hasNext()) {
if(x == it2.next()) {
flag = 1;
break;
}
}
if(flag == 0) {
break;
}
}
/*if(flag == 0) {
System.out.println("NO");
}
else {
System.out.println("YES");
}
*/
input.close();
}
}