中级农民
- 积分
- 115
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-12-7
- 最后登录
- 1970-1-1
|
打卡第一天 六道题(期待各位看官走过路过别忘记高抬贵手~~)
其实7月份天天都在刷题,每天都会写一个长总结,虽然题目不多,但是感觉收获很大,今天开始来地里继续开始打7月的卡了,加油干!
1.Check if Decimal
-注意上来手写5个boolean
-然后从左到右逐个依次进行检查
-最后通过validNumber && numberAfterPoint && numberAfterE来判断到底是不是decimal
2.reverse polish expression
-考察对于数据结构stack的应用
-需要检查开始的时候有咩有两个Integer,然后运算的过程当中如果一次拿不到2个intergers也是要丢异常
-值得注意的是除数如果是0那就要丢算术异常
1.2 两道题可以混在一起考察
3. Merge Two SortedLinkedList
-用两个dummy node
-whoever smaller choose whom
-最后记得断尾
4. Add two number
-想清楚terminal condition
-用int val记录一下当前的数,用取余数的方法新建node然后塞进新的list里面去
-然后每次通过 val /= 10来更新 val自己
5.LRU implementation
High level:
what kind of operations should I implement
what kind of DSs should I use;
what is LRU(least recently used)
Details:
Use a doubly LinkedList and a hashMap
Firstly, initialize the head and tail node;
initialize the treeMap;
and the hashMap take <key, Node> as <key, value> pair;
set(K key, V value)
check if hashMap contains this key,
case 1: already exist remove it from the cache and unlinked key Node in the doubly Linked List, then add the new One just right behind the head, and add the new <Key,Node> pair to the cache.
case 2: not exist
check the size of the cache and if it already full(cache.size() == capacity) then remove the earliest element we set, and unlink that node in the Doubly Linked List, finally create a new ListNode just right behind the head and put it into the cache. If it is not full just add the new Key-value pair into the cache and add the new Node right behind the head node
get(K key) -> return null if key does not exist in the hashMap
6. Max Stack
-虽然可以用两个stacks做,但是这样在用popMax的时候时间复杂度可能会直逼O(N)
-所以仍然采用Doubly‘linkedList + HashMap(treeMap)来做 只是参数会变化一些
High level:
my approach: use doubly-linked List and a treeMap
instead of using two stacks so I can make sure the popMax() operation can quicker than O(n) -> O(logN)
Details:
Firstly, set up the head and tail doubly-linked list node and initialize an empty treeMap;
push(): every time got an element, create a new ListNode and put it just behind the head, also to check if the element has already existed in the treeMap then update the <key, List<Node>> pair
pop(): unlinked the node just behind the head; and remove the node in the treeMap, if there is only one exist in the <key, List<Node>> pair, remove the pair;
top(): return the value of the node right behind the head;
peekMax(): use treeMap.LastKey() just return the value;
popMax(): use treeMap.Lastkey() not only return the value but remove the node in the Doubly linked List, also remove the node in the treeMap, if there is only one exists in the <key, List<Node>> pair, remove the pair;
|
|