leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2191.cpp (860B)


      1 class Solution {
      2   public:
      3     vector<int> sortJumbled(const vector<int> &mapping, vector<int> &nums) const {
      4         const auto shuffle = [&](int a) {
      5             if (a == 0) return mapping[0];
      6 
      7             int res = 0, acc = 1;
      8             while (a > 0) {
      9                 const int digit = a % 10;
     10                 res += acc * mapping[digit];
     11                 acc *= 10;
     12                 a /= 10;
     13             }
     14 
     15             return res;
     16         };
     17 
     18         static int idxs[30001], tmp[30001];
     19         const int n = size(nums);
     20 
     21         for (int i = 0; i < n; i++)
     22             tmp[i] = shuffle(nums[i]);
     23 
     24         iota(idxs, idxs + n, 0);
     25         sort(idxs, idxs + n, [&](int a, int b) { return tmp[a] != tmp[b] ? tmp[a] < tmp[b] : a < b; });
     26 
     27         for (int i = 0; i < n; i++)
     28             tmp[i] = nums[idxs[i]];
     29 
     30         return vector(tmp, tmp + n);
     31     }
     32 };