本文主要是介绍2671. 频率跟踪器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2671. 频率跟踪器
题目链接:2671. 频率跟踪器
代码如下:
class FrequencyTracker {
public:FrequencyTracker() {}void add(int number) {--freq[cnt[number]];//去掉一个旧的cnt[number]++freq[++cnt[number]];//添加一个新的cnt[number]}void deleteOne(int number) {if(!cnt[number]) return;--freq[cnt[number]];//去掉一个旧的cnt[number]++freq[--cnt[number]];//添加一个新的cnt[number]}bool hasFrequency(int frequency) {return freq[frequency];}private:unordered_map<int,int> cnt;//number出现的次数unordered_map<int,int> freq;//number出现次数的出现次数
};/*** Your FrequencyTracker object will be instantiated and called as such:* FrequencyTracker* obj = new FrequencyTracker();* obj->add(number);* obj->deleteOne(number);* bool param_3 = obj->hasFrequency(frequency);*/
这篇关于2671. 频率跟踪器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!