leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2191.cpp (860B)
0 class Solution { 1 public: 2 vector<int> sortJumbled(const vector<int> &mapping, vector<int> &nums) const { 3 const auto shuffle = [&](int a) { 4 if (a == 0) return mapping[0]; 5 6 int res = 0, acc = 1; 7 while (a > 0) { 8 const int digit = a % 10; 9 res += acc * mapping[digit]; 10 acc *= 10; 11 a /= 10; 12 } 13 14 return res; 15 }; 16 17 static int idxs[30001], tmp[30001]; 18 const int n = size(nums); 19 20 for (int i = 0; i < n; i++) 21 tmp[i] = shuffle(nums[i]); 22 23 iota(idxs, idxs + n, 0); 24 sort(idxs, idxs + n, [&](int a, int b) { return tmp[a] != tmp[b] ? tmp[a] < tmp[b] : a < b; }); 25 26 for (int i = 0; i < n; i++) 27 tmp[i] = nums[idxs[i]]; 28 29 return vector(tmp, tmp + n); 30 } 31 };