forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelativeSortArray1.java
More file actions
42 lines (31 loc) · 1.15 KB
/
RelativeSortArray1.java
File metadata and controls
42 lines (31 loc) · 1.15 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
import java.util.*;
import java.util.stream.Collectors;
/**
* Description: 数组的相对位置
* 使用自定义比较函数实现
* Date: 2020-12-27
* Time: 10:54 AM
*/
public class RelativeSortArray1 {
Map<Integer,Integer> record;
public int[] relativeSortArray(int[] arr1, int[] arr2) {
record = new HashMap<>(arr2.length);
for (int i = 0; i < arr2.length; i++) {
record.put(arr2[i], i);
}
List<Integer> res = Arrays.stream(arr1).boxed().collect(Collectors.toList());
Collections.sort(res, (num1, num2) -> {
if (record.containsKey(num1) || record.containsKey(num2)) {
return record.getOrDefault(num1, 1000) - record.getOrDefault(num2, 1000);
} else {
return num1 - num2;
}
});
return res.stream().mapToInt(i -> i.intValue()).toArray();
}
public static void main(String[] args) {
int[] arr1 = new int[]{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19};
int[] arr2 = new int[]{2, 1, 4, 3, 9, 6};
System.out.println(Arrays.toString(new RelativeSortArray1().relativeSortArray(arr1, arr2)));
}
}