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;
    };
     */

    ---

  • 相关阅读:
    Vue 路由组件
    编写第一个JavaScript程序
    JavaScript 介绍
    JavaScript
    前台数据库
    cookie
    js date string parse
    判断时间大小 yyyy-MM-dd 格式
    正则表达式替换
    测试计时器
  • 原文地址:https://www.cnblogs.com/cunyusup/p/14247562.html
Copyright © 2011-2022 走看看