解题思路:
(1)使用unordered_map的键值对存储B的值和位置
(2)对A中的每个数提供查找
class Solution {
public:
/**
* @param A: lists A
* @param B: lists B
* @return: the index mapping
*/
vector<int> anagramMappings(vector<int> &A, vector<int> &B) {
// Write your code here
unordered_map<int,int> mp;
vector<int> res;
for(int i=0;i<B.size();i++) {
mp[B[i]]=i;
}
for(auto w:A) {
res.push_back(mp[w]);
}
return res;
}
};

本文介绍了一种使用C++解决特定问题的方法,即如何在两个数组之间建立元素映射关系。通过使用unordered_map数据结构,我们可以在O(n)的时间复杂度内完成这一任务。首先,将数组B的元素及其索引存入unordered_map中,然后遍历数组A,查找对应元素在B中的位置,并将这些位置信息存储为最终结果。
1218

被折叠的 条评论
为什么被折叠?



