-
Notifications
You must be signed in to change notification settings - Fork 35
/
LeetCode_2191.java
37 lines (33 loc) · 1.26 KB
/
LeetCode_2191.java
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
class Solution {
public int[] sortJumbled(int[] mapping, int[] nums) {
ArrayList<Integer[]> storePairs = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
// Convert current value to string
String number = Integer.toString(nums[i]);
String formed = "";
for (int j = 0; j < number.length(); ++j) {
formed = formed +
Integer.toString(mapping[number.charAt(j) - '0']);
}
// Store the mapped value.
int mappedValue = Integer.parseInt(formed);
// Push a pair consisting of mapped value and original value's index.
storePairs.add(new Integer[] { mappedValue, i });
}
// Sort the array in non-decreasing order by the first value (default).
Collections.sort(
storePairs,
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[0].compareTo(o2[0]);
}
}
);
int[] answer = new int[nums.length];
for (int i = 0; i < storePairs.size(); i++) {
answer[i] = nums[storePairs.get(i)[1]];
}
return answer;
}
}