File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
4
+ vector<int> mapped(nums.size());
5
+ unordered_map<int, list<int>> num_map;
6
+
7
+ for (int i = 0; i < nums.size(); i++) {
8
+ int number = nums[i];
9
+ int idx = 1;
10
+ int mapped_number = 0;
11
+
12
+ while (number > 0) {
13
+ int remainder = number % 10;
14
+ mapped_number += mapping[remainder] * idx;
15
+ number /= 10;
16
+ idx *= 10;
17
+ }
18
+
19
+ if (nums[i] == 0) {
20
+ mapped_number = mapping[0];
21
+ }
22
+
23
+ mapped[i] = mapped_number;
24
+ num_map[mapped[i]].push_back(nums[i]);
25
+ }
26
+
27
+ vector<int> sorted_mapped = mapped;
28
+ sort(sorted_mapped.begin(), sorted_mapped.end());
29
+ int idx = 0;
30
+
31
+ for (int value : sorted_mapped) {
32
+ if (num_map.find(value) != num_map.end()) {
33
+ for (int val : num_map[value]) {
34
+ nums[idx++] = val;
35
+ }
36
+ num_map.erase(value);
37
+ }
38
+ }
39
+
40
+ return nums;
41
+ }
42
+ };
You can’t perform that action at this time.
0 commit comments