Leetcode 1429. 第一个唯一数字

class FirstUnique {
public:
    FirstUnique(vector<int>& nums) {
        for(auto num:nums){
            q.push(num);
            mp[num]++;
        }
    }
    
    int showFirstUnique() {
        while(!q.empty()&&mp[q.front()] > 1){
            q.pop();
        }
        if(q.empty()) return -1;
        return q.front();
    }
    
    void add(int value) {
        q.push(value);
        mp[value]++;
    }
private:
    queue<int> q;
    unordered_map<int,int> mp;
};

/**
 * Your FirstUnique object will be instantiated and called as such:
 * FirstUnique* obj = new FirstUnique(nums);
 * int param_1 = obj->showFirstUnique();
 * obj->add(value);



 class FirstUnique {
public:
    FirstUnique(vector<int>& nums) {
        for (auto& num : nums) {
            q.push(num);
            m[num]++;
        }
    }
    
    int showFirstUnique() {
        while (!q.empty() && m[q.front()] > 1) {
            q.pop();
        }
        if (q.empty()) return -1;
        return q.front();
    }
    
    void add(int value) {
        q.push(value);
        m[value]++;
    }
private:
    queue<int> q;
    unordered_map<int, int> m;
};
 */

---

原文地址:https://www.cnblogs.com/cunyusup/p/14247562.html