leetcode算法题--将杂乱无章的数字排序

原题链接:https://leetcode.cn/problems/sort-the-jumbled-numbers/

vector<int> mp;
int getMap(int a) {
    int x = 0, m = 1;
    do {
        int t = a%10;
        a /= 10;
        x += mp[t]*m;
        m *= 10;
    } while(a);
    return x;
}
bool cmp(int a, int b) {
    return getMap(a) < getMap(b);
}

class Solution {
public:
    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
        mp = mapping;
        sort(nums.begin(), nums.end(), cmp);
        return nums;
    }
};

你可能感兴趣的:(Algorithm,leetcode,算法,排序算法)