forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
32 lines (30 loc) · 815 Bytes
/
Solution.rs
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
impl Solution {
pub fn sort_jumbled(mapping: Vec<i32>, nums: Vec<i32>) -> Vec<i32> {
let f = |x: i32| -> i32 {
if x == 0 {
return mapping[0];
}
let mut y = 0;
let mut k = 1;
let mut num = x;
while num != 0 {
let v = mapping[(num % 10) as usize];
y = k * v + y;
k *= 10;
num /= 10;
}
y
};
let n = nums.len();
let mut arr: Vec<(i32, usize)> = Vec::with_capacity(n);
for i in 0..n {
arr.push((f(nums[i]), i));
}
arr.sort();
let mut ans: Vec<i32> = Vec::with_capacity(n);
for (_, i) in arr {
ans.push(nums[i]);
}
ans
}
}