[LeetCode] 1429. First Unique Number


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

題意是設計一個FirstUnique的class,包含一開始列出的三個函數。可以發現,因為沒有刪除操作,加入queue的數字從未被拿出來,但是每次需要返回的是第一個unique的數字。既然是找unique的數字,必然會牽涉到hashmap,所以我的思路是用一個hashmap記錄每個數字及其他們各自的出現次數,同時創建一個queue,在做add操作的時候也是無條件將元素加入queue。但是在showFirstUnique中,如果queue里面頭一個節點(peek)不是unique的,則彈出這個元素,直到找到一個unique的元素。這個思路可行但是時間復雜度不太理想。

時間O(n) - 有可能queue需要彈出很多元素之后才能找的到第一個unique value

空間O(n)

Java實現

 1 class FirstUnique {
 2     HashMap<Integer, Integer> map;
 3     Queue<Integer> queue;
 4     
 5     public FirstUnique(int[] nums) {
 6         map = new HashMap<>();
 7         queue = new LinkedList<>();
 8         for (int num : nums) {
 9             add(num);
10         }
11     }
12     
13     public int showFirstUnique() {
14         while (!queue.isEmpty()) {
15             int num = queue.peek();
16             int freq = map.get(num);
17             if (freq > 1) {
18                 queue.poll();
19             } else {
20                 return num;
21             }
22         }
23         return -1;
24     }
25     
26     public void add(int value) {
27         if (map.containsKey(value)) {
28             map.put(value, map.get(value) + 1);
29         } else {
30             map.put(value, 1);
31             queue.add(value);
32         }
33     }
34 }
35 
36 /**
37  * Your FirstUnique object will be instantiated and called as such:
38  * FirstUnique obj = new FirstUnique(nums);
39  * int param_1 = obj.showFirstUnique();
40  * obj.add(value);
41  */

 

網上看到一個更好的解法,思路差不多,但是用到了LinkedHashSet幫助記錄unique value。思路是用一個hashset和一個LinkedHashSet記錄元素,hashset首先還是在記錄獨一的元素,當遍歷到重復元素后,hashset也只會返回false;LinkedHashSet因為是有序的,所以當遍歷到重復元素的時候,需要把這個元素從LinkedHashSet中刪去。這樣在返回unique value的時候,時間上會省很多。

時間O(n),雖然返回unique的動作是O(1)但是add()函數在極端情況下也會很費時間

空間O(n)

Java實現

 1 class FirstUnique {
 2     Set<Integer> unique = new LinkedHashSet<>();
 3     Set<Integer> all = new HashSet<>();
 4     
 5     public FirstUnique(int[] nums) {
 6         for (int num : nums) {
 7             add(num);
 8         }
 9     }
10     
11     public int showFirstUnique() {
12         if (unique.isEmpty()) {
13             return -1;
14         }
15         return unique.iterator().next();
16     }
17     
18     public void add(int value) {
19         if (all.add(value)) {
20             unique.add(value);
21         } else {
22             unique.remove(value);
23         }
24     }
25 }
26 
27 /**
28  * Your FirstUnique object will be instantiated and called as such:
29  * FirstUnique obj = new FirstUnique(nums);
30  * int param_1 = obj.showFirstUnique();
31  * obj.add(value);
32  */

 

LeetCode 題目總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM