放假沒事也來寫寫看,真的好難
class Solution {
public:
vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
set<Jumble, JumbleCmp> jumbleList;
for (size_t i = 0; i < nums.size(); i++) {
jumbleList.insert(toJumble(nums[i], i, mapping));
}
vector<int> result;
result.reserve(nums.size());
for (const Jumble& jumble : jumbleList) {
result.push_back(jumble.original);
}
return result;
}
private:
struct Jumble {
int original;
int jumbled;
size_t order;
};
struct JumbleCmp {
bool operator()(const Jumble& lhs, const Jumble& rhs) const {
if (lhs.jumbled == rhs.jumbled) {
return lhs.order < rhs.order;
}
return lhs.jumbled < rhs.jumbled;
}
};
Jumble toJumble(int value, size_t order, const vector<int>& mapping) {
int original = value;
int result = 0;
int factor = 1;
do {
int currentValue = value % 10;
result += mapping[currentValue] * factor;
value /= 10;
factor *= 10;
} while (value != 0);
return {original, result, order};
}
};