|
| 1 | +<p>You have a queue of integers, you need to retrieve the first unique integer in the queue.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>FirstUnique</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>FirstUnique(int[] nums)</code> Initializes the object with the numbers in the queue.</li> |
| 7 | + <li><code>int showFirstUnique()</code> returns the value of <strong>the first unique</strong> integer of the queue, and returns <strong>-1</strong> if there is no such integer.</li> |
| 8 | + <li><code>void add(int value)</code> insert value to the queue.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<b>Input: </b> |
| 16 | +["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"] |
| 17 | +[[[2,3,5]],[],[5],[],[2],[],[3],[]] |
| 18 | +<b>Output: </b> |
| 19 | +[null,2,null,2,null,3,null,-1] |
| 20 | +<b>Explanation: </b> |
| 21 | +FirstUnique firstUnique = new FirstUnique([2,3,5]); |
| 22 | +firstUnique.showFirstUnique(); // return 2 |
| 23 | +firstUnique.add(5); // the queue is now [2,3,5,5] |
| 24 | +firstUnique.showFirstUnique(); // return 2 |
| 25 | +firstUnique.add(2); // the queue is now [2,3,5,5,2] |
| 26 | +firstUnique.showFirstUnique(); // return 3 |
| 27 | +firstUnique.add(3); // the queue is now [2,3,5,5,2,3] |
| 28 | +firstUnique.showFirstUnique(); // return -1 |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 2:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<b>Input: </b> |
| 35 | +["FirstUnique","showFirstUnique","add","add","add","add","add","showFirstUnique"] |
| 36 | +[[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]] |
| 37 | +<b>Output: </b> |
| 38 | +[null,-1,null,null,null,null,null,17] |
| 39 | +<b>Explanation: </b> |
| 40 | +FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]); |
| 41 | +firstUnique.showFirstUnique(); // return -1 |
| 42 | +firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7] |
| 43 | +firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3] |
| 44 | +firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3,3] |
| 45 | +firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7,3,3,7] |
| 46 | +firstUnique.add(17); // the queue is now [7,7,7,7,7,7,7,3,3,7,17] |
| 47 | +firstUnique.showFirstUnique(); // return 17 |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p><strong class="example">Example 3:</strong></p> |
| 51 | + |
| 52 | +<pre> |
| 53 | +<b>Input: </b> |
| 54 | +["FirstUnique","showFirstUnique","add","showFirstUnique"] |
| 55 | +[[[809]],[],[809],[]] |
| 56 | +<b>Output: </b> |
| 57 | +[null,809,null,-1] |
| 58 | +<b>Explanation: </b> |
| 59 | +FirstUnique firstUnique = new FirstUnique([809]); |
| 60 | +firstUnique.showFirstUnique(); // return 809 |
| 61 | +firstUnique.add(809); // the queue is now [809,809] |
| 62 | +firstUnique.showFirstUnique(); // return -1 |
| 63 | +</pre> |
| 64 | + |
| 65 | +<p> </p> |
| 66 | +<p><strong>Constraints:</strong></p> |
| 67 | + |
| 68 | +<ul> |
| 69 | + <li><code>1 <= nums.length <= 10^5</code></li> |
| 70 | + <li><code>1 <= nums[i] <= 10^8</code></li> |
| 71 | + <li><code>1 <= value <= 10^8</code></li> |
| 72 | + <li>At most <code>50000</code> calls will be made to <code>showFirstUnique</code> and <code>add</code>.</li> |
| 73 | +</ul> |
0 commit comments