Find Anagram Mappings(C++ LintCode)

解题思路:

(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;
    }
};
发布了302 篇原创文章 · 获赞 277 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/105658024