zoukankan      html  css  js  c++  java
  • 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;
    };
     */

    ---

  • 相关阅读:
    PKU 3984 迷宫问题
    九度 1341 艾薇儿的演唱会
    九度 1335
    SDUT 1198 鞍点计算
    POJ 1363 Rails
    SDUT 1570 C旅行
    HDU 1042 N!
    SDUT 1568 俄罗斯方块
    HDU 1257 最少拦截系统
    POJ 3750 小孩报数问题
  • 原文地址:https://www.cnblogs.com/cunyusup/p/14247562.html
Copyright © 2011-2022 走看看