Skip to content

Latest commit

 

History

History
181 lines (143 loc) · 5.24 KB

File metadata and controls

181 lines (143 loc) · 5.24 KB

English Version

题目描述

给定一系列整数,插入一个队列中,找出队列中第一个唯一整数。

实现 FirstUnique 类:

  • FirstUnique(int[] nums) 用数组里的数字初始化队列。
  • int showFirstUnique() 返回队列中的 第一个唯一 整数的值。如果没有唯一整数,返回 -1。(译者注:此方法不移除队列中的任何元素)
  • void add(int value) 将 value 插入队列中。

 

示例 1:

输入:
["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"]
[[[2,3,5]],[],[5],[],[2],[],[3],[]]
输出:
[null,2,null,2,null,3,null,-1]
解释:
FirstUnique firstUnique = new FirstUnique([2,3,5]);
firstUnique.showFirstUnique(); // 返回 2
firstUnique.add(5);            // 此时队列为 [2,3,5,5]
firstUnique.showFirstUnique(); // 返回 2
firstUnique.add(2);            // 此时队列为 [2,3,5,5,2]
firstUnique.showFirstUnique(); // 返回 3
firstUnique.add(3);            // 此时队列为 [2,3,5,5,2,3]
firstUnique.showFirstUnique(); // 返回 -1

示例 2:

输入:
["FirstUnique","showFirstUnique","add","add","add","add","add","showFirstUnique"]
[[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]]
输出:
[null,-1,null,null,null,null,null,17]
解释:
FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]);
firstUnique.showFirstUnique(); // 返回 -1
firstUnique.add(7);            // 此时队列为 [7,7,7,7,7,7,7]
firstUnique.add(3);            // 此时队列为 [7,7,7,7,7,7,7,3]
firstUnique.add(3);            // 此时队列为 [7,7,7,7,7,7,7,3,3]
firstUnique.add(7);            // 此时队列为 [7,7,7,7,7,7,7,3,3,7]
firstUnique.add(17);           // 此时队列为 [7,7,7,7,7,7,7,3,3,7,17]
firstUnique.showFirstUnique(); // 返回 17

示例 3:

输入:
["FirstUnique","showFirstUnique","add","showFirstUnique"]
[[[809]],[],[809],[]]
输出:
[null,809,null,-1]
解释:
FirstUnique firstUnique = new FirstUnique([809]);
firstUnique.showFirstUnique(); // 返回 809
firstUnique.add(809);          // 此时队列为 [809,809]
firstUnique.showFirstUnique(); // 返回 -1

 

提示:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^8
  • 1 <= value <= 10^8
  • 最多调用 5000 次 showFirstUnique 和 add

解法

“有序哈希表”实现。

Python3

class FirstUnique:
    def __init__(self, nums: List[int]):
        self.counter = OrderedDict()
        self.unique_nums = OrderedDict()
        for num in nums:
            self.counter[num] = self.counter.get(num, 0) + 1
        for k, v in self.counter.items():
            if v == 1:
                self.unique_nums[k] = 1

    def showFirstUnique(self) -> int:
        if len(self.unique_nums) == 0:
            return -1
        for k in self.unique_nums.keys():
            return k

    def add(self, value: int) -> None:
        if value not in self.counter:
            self.counter[value] = 1
            self.unique_nums[value] = 1
        else:
            self.counter[value] += 1
            if value in self.unique_nums:
                self.unique_nums.pop(value)


# Your FirstUnique object will be instantiated and called as such:
# obj = FirstUnique(nums)
# param_1 = obj.showFirstUnique()
# obj.add(value)

Java

class FirstUnique {
    private Map<Integer, Integer> counter;
    private Set<Integer> uniqueNums;

    public FirstUnique(int[] nums) {
        counter = new LinkedHashMap<>();
        uniqueNums = new LinkedHashSet<>();
        for (int num : nums) {
            counter.put(num, counter.getOrDefault(num, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
            if (entry.getValue() == 1) {
                uniqueNums.add(entry.getKey());
            }
        }
    }

    public int showFirstUnique() {
        return uniqueNums.isEmpty() ? -1 : uniqueNums.iterator().next();
    }

    public void add(int value) {
        if (!counter.containsKey(value)) {
            counter.put(value, 1);
            uniqueNums.add(value);
        } else {
            counter.put(value, counter.get(value) + 1);
            uniqueNums.remove(value);
        }
    }
}

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

...