LRU

map + slice

go 利用map + slice + copy() 实现。效率低下

  1. func LRU( op [][]int , maxLen int ) (ret []int) {
  2. mp := map[int]int{}
  3. lru := []int{} // 存key
  4. for _, pair := range op {
  5. if pair[0] == 1 {
  6. k, v := pair[1], pair[2]
  7. if _, ok := mp[k]; ok {
  8. mp[k] = v
  9. } else {
  10. if len(lru) < maxLen {
  11. mp[k] = v
  12. lru = append(lru, k)
  13. } else {
  14. delete(mp, lru[0])
  15. copy(lru, lru[1:])
  16. lru[len(lru)-1] = k
  17. mp[k] = v
  18. }
  19. }
  20. } else if pair[0] == 2 {
  21. k := pair[1]
  22. //命中
  23. if v, ok := mp[k]; ok {
  24. ret = append(ret, v)
  25. index := -1
  26. for i:=0; i<len(lru); i++ {
  27. if lru[i] == k {
  28. index = i
  29. break
  30. }
  31. }
  32. copy(lru[index:], lru[index+1:])
  33. lru[len(lru)-1] = k
  34. } else {
  35. ret = append(ret, -1)
  36. }
  37. }
  38. }
  39. return
  40. }
  41. package main
  42. /**
  43. * lru design
  44. * @param operators int整型二维数组 the ops
  45. * @param k int整型 the k
  46. * @return int整型一维数组
  47. */
  48. func LRU( op [][]int , maxLen int ) (ret []int) {
  49. mp := map[int]int{}
  50. lru := []int{} // 存key
  51. for _, pair := range op {
  52. if pair[0] == 1 {
  53. k, v := pair[1], pair[2]
  54. if _, ok := mp[k]; ok {
  55. mp[k] = v
  56. } else {
  57. if len(lru) < maxLen {
  58. mp[k] = v
  59. lru = append(lru, k)
  60. } else {
  61. delete(mp, lru[0])
  62. lru = append(lru[1:], k)
  63. mp[k] = v
  64. }
  65. }
  66. } else if pair[0] == 2 {
  67. k := pair[1]
  68. //命中
  69. if v, ok := mp[k]; ok {
  70. ret = append(ret, v)
  71. index := -1
  72. for i:=0; i<len(lru); i++ {
  73. if lru[i] == k {
  74. index = i
  75. break
  76. }
  77. }
  78. lru = append(lru[:index], lru[index+1:]...)
  79. lru = append(lru, k)
  80. } else {
  81. ret = append(ret, -1)
  82. }
  83. }
  84. }
  85. return
  86. }

哈希表 + 双链表

  1. type Node struct {
  2. Key, Val int
  3. Prev, Next *Node
  4. }
  5. type LRUCache struct {
  6. Mp map[int]*Node
  7. Cap int
  8. Head, Tail *Node
  9. }
  10. func (l *LRUCache) addNode(node *Node) {
  11. node.Next = l.Head.Next
  12. l.Head.Next.Prev = node
  13. l.Head.Next = node
  14. node.Prev = l.Head
  15. }
  16. func (l *LRUCache) deleteNode(node *Node) {
  17. node.Next.Prev = node.Prev
  18. node.Prev.Next = node.Next
  19. }
  20. func (l *LRUCache) moveToHead(node *Node) {
  21. l.deleteNode(node)
  22. l.addNode(node)
  23. }
  24. func Constructor(capacity int) LRUCache {
  25. head, tail := &Node{}, &Node{}
  26. head.Next = tail
  27. tail.Prev = head
  28. return LRUCache {
  29. Mp: map[int]*Node{},
  30. Cap: capacity,
  31. Head: head,
  32. Tail: tail,
  33. }
  34. }
  35. func (l *LRUCache) Get(key int) int {
  36. if v, ok := l.Mp[key]; ok {
  37. l.moveToHead(v)
  38. return v.Val
  39. }
  40. return -1
  41. }
  42. func (l *LRUCache) Put(key int, value int) {
  43. if v, ok := l.Mp[key]; ok {
  44. v.Val = value
  45. l.moveToHead(v)
  46. } else {
  47. node := &Node{Key: key, Val: value}
  48. l.addNode(node)
  49. l.Mp[key] = node
  50. }
  51. if len(l.Mp) > l.Cap {
  52. delete(l.Mp, l.Tail.Prev.Key)
  53. l.deleteNode(l.Tail.Prev)
  54. }
  55. }

list.List + map

  1. import "container/list"
  2. type LRUCache struct {
  3. cache map[int]*list.Element
  4. ll *list.List
  5. cap int
  6. }
  7. func Constructor(capacity int) LRUCache {
  8. return LRUCache {
  9. cache: make(map[int]*list.Element),
  10. ll: list.New(),
  11. cap: capacity,
  12. }
  13. }
  14. type node struct {
  15. key, value int
  16. }
  17. func (this *LRUCache) Get(key int) int {
  18. m := this.cache
  19. if v, ok := m[key]; ok {
  20. ele := v.Value.(*node)
  21. this.ll.MoveToFront(v)
  22. return ele.value
  23. }
  24. return -1
  25. }
  26. func (this *LRUCache) Put(key int, value int) {
  27. m := this.cache
  28. if v, ok := m[key]; ok {
  29. v.Value.(*node).value = value
  30. this.ll.MoveToFront(v)
  31. return
  32. }
  33. if len(m) >= this.cap {
  34. v := this.ll.Back()
  35. ele := v.Value.(*node)
  36. delete(m, ele.key)
  37. ele.key, ele.value = key, value
  38. m[key] = v
  39. this.ll.MoveToFront(v)
  40. return
  41. }
  42. ele := &node{key: key, value: value}
  43. v := this.ll.PushFront(ele)
  44. m[key] = v
  45. }
  46. /**
  47. * Your LRUCache object will be instantiated and called as such:
  48. * obj := Constructor(capacity);
  49. * param_1 := obj.Get(key);
  50. * obj.Put(key,value);
  51. */

LFU

lfu.png

  1. type node struct {
  2. key int
  3. val int
  4. count int
  5. prev *node
  6. next *node
  7. }
  8. func (n *node) del() {
  9. n.next.prev = n.prev
  10. n.prev.next = n.next
  11. }
  12. type dequeue struct {
  13. head *node
  14. tail *node
  15. length int
  16. }
  17. // delete from head
  18. func (dq *dequeue) deleteNode() *node {
  19. head := dq.head
  20. n := head.next
  21. n.next.prev = head
  22. head.next = n.next
  23. dq.length--
  24. n.next, n.prev = nil, nil
  25. return n
  26. }
  27. // add to tail
  28. func (dq *dequeue) addNode(n *node) {
  29. dq.tail.prev.next = n
  30. n.prev = dq.tail.prev
  31. n.next = dq.tail
  32. dq.tail.prev = n
  33. dq.length++
  34. }
  35. type LFUCache struct {
  36. k2node map[int]*node // key 到 节点的映射
  37. cache map[int]*dequeue // 次数 到 双链表的映射
  38. scores *dequeue
  39. length int
  40. captiy int
  41. }
  42. func (l *LFUCache) add(k int, n *node) {
  43. if l.length == l.captiy {
  44. l.goOut()
  45. }
  46. if b, ok := l.cache[k]; ok {
  47. b.addNode(n)
  48. } else {
  49. newb := &dequeue{}
  50. newb.head, newb.tail = &node{}, &node{}
  51. newb.head.next = newb.tail
  52. newb.tail.prev = newb.head
  53. newb.addNode(n)
  54. l.cache[k] = newb
  55. }
  56. l.length++
  57. }
  58. func (l *LFUCache) goOut() {
  59. mink, minv := math.MaxInt32, (*dequeue)(nil)
  60. for k, v := range l.cache {
  61. if k < mink && v.length > 0 {
  62. mink = k
  63. minv = v
  64. }
  65. }
  66. n := minv.deleteNode()
  67. delete(l.k2node, n.key)
  68. l.length--
  69. }
  70. func Constructor(capacity int) LFUCache {
  71. return LFUCache {
  72. captiy: capacity,
  73. k2node: make(map[int]*node),
  74. cache: make(map[int]*dequeue),
  75. }
  76. }
  77. func (l *LFUCache) Get(k int) int {
  78. if l.captiy == 0 {
  79. return -1
  80. }
  81. if n, ok := l.k2node[k]; ok {
  82. val := n.val
  83. b := l.cache[n.count]
  84. n.del()
  85. b.length--
  86. l.length--
  87. n.count++
  88. l.add(n.count, n)
  89. return val
  90. }
  91. return -1
  92. }
  93. func (l *LFUCache) Put(k int, v int) {
  94. if l.captiy == 0 {
  95. return
  96. }
  97. if n, ok := l.k2node[k]; ok {
  98. n.val = v
  99. b := l.cache[n.count]
  100. n.del()
  101. b.length--
  102. l.length--
  103. n.count++
  104. l.add(n.count, n)
  105. } else {
  106. newNode := &node{key: k, val: v, count: 1}
  107. l.k2node[k] = newNode
  108. l.add(newNode.count, newNode)
  109. }
  110. }
  111. /**
  112. * Your LFUCache object will be instantiated and called as such:
  113. * obj := Constructor(capacity);
  114. * param_1 := obj.Get(key);
  115. * obj.Put(key,value);
  116. */