[Med] 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 -1Example 2:

Example 2:

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

Example 3:

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

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.

Tag: Map, Set, Double-LinkedList
解题思路

这道题目还是挺有意思的,是一道新的题目。题目的意思是说,给我们一个初始的数组。当程序调用showFirstUnique()的时候,要返回第一个按照顺序获得的,且unique的数字。unique的意思是说这个数字到目前为止没有出现过超过一次。我们会持续的调用add()方法添加数字,注意我们初始化的时候就会往里面添加进一个数组。往后添加的方式和第一次的添加方式是一样的。注意,这里一开始我没有搞明白结果写错了。这里我们给所有元素排序的依据是按照他们被添加进来的顺序,每一次showFirstUnique()也是按照这个顺序找数字的,一开始我以为是找最小的数字,后来才发现不是。

其实这个题目跟LRU特别像,但是没有那么难。我们也是建立一个Node节点,做成double linkedlist的形态。然后使用一个hashmap在O(1)时间内可以定位每个元素在什么位置。每一次添加元素我们都添加在链表的尾部。我们每一次获取元素都获取链表头部的元素。如果某一个元素出现了两次,那么我们就从链表和hashmap当中删除这个元素。同时移到一个叫做used的hashset()当中。这样我们就可以快速的知道是元素出现过。

这样增加,删除,getFirst()的时间都是O(1)
时间复杂度取决于元素的数量。

解法一:

class FirstUnique {Map<Integer, Node> map;Node head, tail;Set<Integer> used = new HashSet();public FirstUnique(int[] nums) {        map = new HashMap<>();head = new Node(0);tail = new Node(0);head.next = tail;tail.prev = head;for(int num: nums) add(num);}public int showFirstUnique() {if(head.next == tail) return -1;else return head.next.val;}public void add(int value) {if(used.contains(value)) return;if(map.containsKey(value)){removeNodeFromMapAndList(value);}else{addElement(value);}}public void removeNodeFromMapAndList(int value){Node node = map.get(value);node.prev.next = node.next;node.next.prev = node.prev;map.remove(value);used.add(value);}public void addElement(int value){Node newNode = new Node(value);Node previous = tail.prev;newNode.next = tail;tail.prev = newNode;newNode.prev = previous;previous.next = newNode;map.put(value, newNode);}class Node{Node next=null, prev=null;int val;public Node(int val){this.val = val;}}}/*** Your FirstUnique object will be instantiated and called as such:* FirstUnique obj = new FirstUnique(nums);* int param_1 = obj.showFirstUnique();* obj.add(value);*/

LeetCode 1429. First Unique Number相关推荐

  1. leetcode 321 Create Max Number

    leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...

  2. js 数组倒序_我用JS刷LeetCode | Day 6 | Palindrome Number

    来公众号「九零后重庆崽儿」,我们一起学前端 回文数: 说明:现阶段的解题暂未考虑复杂度问题 首发地址: 我用JS刷LeetCode | Day 6 | Palindrome Number​www.br ...

  3. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  4. LeetCode(9) - Palindrome Number

    题目要求判断一个整数是不是回文数,假设输入是1234321,就返回true,输入的是123421,就返回false.题目要求in-place,思路其实很简单,在LeetCode(7)里面我们刚好做了r ...

  5. LeetCode - Medium - 264. Ugly Number II

    Topic Math Dynamic Programming Heap Description https://leetcode.com/problems/ugly-number-ii/ Analys ...

  6. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  7. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  8. LeetCode: 387. First Unique Character in a String

    051105 题目 Given a string, find the first non-repeating character in it and return it's index. If it ...

  9. Leetcode Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...

  10. leetcode算法题--Unique Paths II

    原题链接:https://leetcode.com/problems/unique-paths-ii/ class Solution {public:int uniquePathsWithObstac ...

最新文章

  1. 5.7-基于Binlog+Position的复制搭建
  2. 基于蔡氏混沌电路进行非线性共振探究
  3. Navicat连接mysql数据库
  4. 阿里云学生计划领取攻略
  5. 服务器升级内存跟cpu之后性能更差,云服务器cpu重要还是内存重要
  6. HDU 5608 function (杜教筛)
  7. 第2关:HDFS-JAVA接口之读取文件
  8. ssm中使用slf4g
  9. JVM可生成的最大Thread数量探索
  10. tde数据库加密_如何将TDE加密的用户数据库添加到Always On可用性组
  11. [译] 我多希望在我学习 React.js 之前就已经知晓这些小窍门
  12. HDU4506 小明系列故事——师兄帮帮忙【水题】
  13. 使用JAVA实现PL0语言的词法分析器
  14. 数据挖掘机器学习及其他领域数据集汇总
  15. 打开HFSS出现Unable to detect installed products的报错,要求make sure that config/admin.xml exists
  16. php 对象教程,创建一个简单的PHP对象_PHP教程
  17. 机械/电信/生物/化学专业出身,为啥都要转行计算机?
  18. Oracle 中select XX_id_seq.nextval from dual 什么意思呢?
  19. thingsboard如何维护设备的状态的
  20. android diy固件,官方固件不给力?咱自己DIY!手把手教你修改固件!

热门文章

  1. win10浏览器闪退_Win10电脑的自带浏览器闪退怎么办?其实简单四步就可解决!...
  2. 图像算法工作感想之笨方法
  3. Eclipse中去掉代码中的警告Warn
  4. 全国计算机专业大学排名全部,全国计算机专业大学排名汇总
  5. 白嫖UltraEdit、UltraCompare等等类似工具(2021.2.16更新)
  6. 修复win7更新服务器失败,Win7旗舰版系统Windows Update更新提示遇到未知错误的解决方法...
  7. 电脑公司Windows7_X86旗舰版V0911
  8. C语言:输出菱形图案
  9. 混合高斯模型Gaussian Mixture Model(GMM)的EM(Expectation Maxium)求解代码
  10. 计算机切换用户界面,win7系统登录界面切换用户的方法