Leetcode - 第一个唯一数字

229 阅读1分钟

这是我参与更文挑战的第2天,活动详情查看更文挑战

问题描述

You have a queue of integers, you need to retrieve the first unique integer in the queue.

Implement the FirstUnique class:

FirstUnique(int[] nums) Initializes the object with the numbers in the queue.

int showFirstUnique() returns the value of the first unique integer of the queue, and returns -1 if there is no such integer.

void add(int value) insert value to the queue.

Example 1:

Input: 
    ["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"]
    [[[2,3,5]],[],[5],[],[2],[],[3],[]]
Output: 
    [null,2,null,2,null,3,null,-1]

Explanation: 
    FirstUnique firstUnique = new FirstUnique([2,3,5]);
    firstUnique.showFirstUnique(); // return 2
    firstUnique.add(5);            // the queue is now [2,3,5,5]
    firstUnique.showFirstUnique(); // return 2
    firstUnique.add(2);            // the queue is now [2,3,5,5,2]
    firstUnique.showFirstUnique(); // return 3
    firstUnique.add(3);            // the queue is now [2,3,5,5,2,3]
    firstUnique.showFirstUnique(); // return -1

Example 2:

Input: 
    ["FirstUnique","showFirstUnique","add","add","add","add","add","showFirstUnique"]
    [[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]]
Output: 
    [null,-1,null,null,null,null,null,17]

Explanation: 
    FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]);
    firstUnique.showFirstUnique(); // return -1
    firstUnique.add(7);            // the queue is now [7,7,7,7,7,7,7]
    firstUnique.add(3);            // the queue is now [7,7,7,7,7,7,7,3]
    firstUnique.add(3);            // the queue is now [7,7,7,7,7,7,7,3,3]
    firstUnique.add(7);            // the queue is now [7,7,7,7,7,7,7,3,3,7]
    firstUnique.add(17);           // the queue is now [7,7,7,7,7,7,7,3,3,7,17]
    firstUnique.showFirstUnique(); // return 17

Example 3:

Input: 
    ["FirstUnique","showFirstUnique","add","showFirstUnique"]
    [[[809]],[],[809],[]]
Output: 
    [null,809,null,-1]

Explanation: 
    FirstUnique firstUnique = new FirstUnique([809]);
    firstUnique.showFirstUnique(); // return 809
    firstUnique.add(809);          // the queue is now [809,809]
    firstUnique.showFirstUnique(); // return -1

Constraints:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^8
  • 1 <= value <= 10^8
  • At most 50000 calls will be made to showFirstUnique and add.

有一个整数队列,需要检索队列中的第一个唯一整数。

实现FirstUnique类:

FirstUnique(int [] nums)使用队列中的数字初始化对象。

int showFirstUnique()返回队列的第一个唯一数字的值,如果没有这样的整数则返回-1

void add(int value)把数字插入队列中

限制:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^8
  • 1 <= value <= 10^8
  • 最多对showFirstUniqueadd进行5000次调用

解题思路

问题主要是优化时间,避免时间过长。

初始化明显直接调用add就可以了,showFirstUnique当中,把结果放到数列的开头直接拿才最快,add中包括了查询操作所以需要用到哈希map

综上,只需要一个链表一个哈希map就行了

代码

C++ 版本

class FirstUnique {
public:
    list<int> unique_list;
    unordered_map<int, list<int>::iterator> unique_map;
    FirstUnique(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i++){
            add(nums[i]);
        }
    }
    
    int showFirstUnique() {
        if(unique_list.size() <= 0)
            return -1;
        return *unique_list.begin();
    }
    
    void add(int value) {
        auto it = unique_map.find(value);
        if(it == unique_map.end()){ // not found
            unique_list.push_back(value);
            unique_map.insert(make_pair(value, --unique_list.end()));
        }
        else if(it->second != unique_list.end()){ //found
            unique_list.erase(it->second);
            it->second = unique_list.end();
        }
    }
};
 
/**
 * Your FirstUnique object will be instantiated and called as such:
 * FirstUnique* obj = new FirstUnique(nums);
 * int param_1 = obj->showFirstUnique();
 * obj->add(value);
 */