LeetCode面试题 16.02: 单词频率

First. Problem’s Description

在这里插入图片描述

Second. Problem’s Solution

We can simply use a unordered_map to solve this problem, which core is powered by Hash
By the way, the default initial value is 0 0

Three. Code For Solution

class WordsFrequency {
private:
    unordered_map<string, int> ump;
public:
    WordsFrequency(vector<string>& book) {
        for(auto it: book)  ump[it]++;            
    }
    
    int get(string word) {
        return ump[word];
    }
};

Four. Processing Results

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44587168/article/details/106085116