-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1460.java
More file actions
37 lines (32 loc) · 1.32 KB
/
LeetCode1460.java
File metadata and controls
37 lines (32 loc) · 1.32 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
import java.util.HashMap;
public class LeetCode1460 {
public static void main(String[] args) {
// 输入:target = [1,2,3,4], arr = [2,4,1,3]
// 输出:true
System.out.println(new Solution1460().canBeEqual(new int[] { 1, 2, 3, 4 }, new int[] { 2, 4, 1, 3 }));
// 输入:target = [7], arr = [7]
// 输出:true
System.out.println(new Solution1460().canBeEqual(new int[] { 7 }, new int[] { 7 }));
// 输入:target = [3,7,9], arr = [3,7,11]
// 输出:false
System.out.println(new Solution1460().canBeEqual(new int[] { 3, 7, 9 }, new int[] { 3, 7, 11 }));
// 输入:target = [1,2,2,3], arr = [1,1,2,3]
// 输出:false
System.out.println(new Solution1460().canBeEqual(new int[] { 1, 2, 2, 3 }, new int[] { 1, 1, 2, 3 }));
}
}
class Solution1460 {
public boolean canBeEqual(int[] target, int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < target.length; i++) {
map.put(target[i], map.getOrDefault(target[i], 0) + 1);
}
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i]) || map.get(arr[i]) == 0) {
return false;
}
map.put(arr[i], map.get(arr[i]) - 1);
}
return true;
}
}