题意:

image.png

解题思路:

  1. 思路:LRU (最近最少使用)
  2. 1. 初始化容量,cache,大小;
  3. 2. 获取key时,如果存在key,则先取出,然后删除对应的key,同时将元素插入在数组的最前面;
  4. 3. 插入值时,如果缓存中存在要插入的key,则删除对应的key,同时将新的key前移[即插在数组最前面];
  5. 4. 如果不存在,则先判断容量,如果超出最大容量值,则先删除数组末尾元素[末尾元素代表最近很少使用]对应的key,如果为['a'=>'','b' => '','c' => '']则删除a对应的元素值,再将元素插入在数组最前面['b' => '','c' => '','a'=>'']

PHP代码实现:

  1. class LRUCache {
  2. private $capacity;
  3. private $size;
  4. private $cache = [];
  5. /**
  6. * @param Integer $capacity
  7. */
  8. function __construct($capacity) {
  9. $this->capacity = $capacity;
  10. }
  11. /**
  12. * @param Integer $key
  13. * @return Integer
  14. */
  15. function get($key) {
  16. if (isset($this->cache[$key])) {
  17. $value = $this->cache[$key];
  18. unset($this->cache[$key]);
  19. $this->cache[$key] = $value;
  20. return $value;
  21. }
  22. return -1;
  23. }
  24. /**
  25. * @param Integer $key
  26. * @param Integer $value
  27. * @return NULL
  28. */
  29. function put($key, $value) {
  30. if (isset($this->cache[$key])) {
  31. unset($this->cache[$key]);
  32. $this->cache[$key] = $value;
  33. } else {
  34. if ($this->size < $this->capacity) {
  35. $this->size++;
  36. } else {
  37. $curKey = key($this->cache);
  38. unset($this->cache[$curKey]);
  39. }
  40. $this->cache[$key] = $value;
  41. }
  42. }
  43. }
  44. /**
  45. * Your LRUCache object will be instantiated and called as such:
  46. * $obj = LRUCache($capacity);
  47. * $ret_1 = $obj->get($key);
  48. * $obj->put($key, $value);
  49. */

GO代码实现:

  1. type LRUCache struct {
  2. Cap int
  3. Map map[int]*Node
  4. Head *Node
  5. Last *Node
  6. }
  7. type Node struct {
  8. Val int
  9. Key int
  10. Pre *Node
  11. Next *Node
  12. }
  13. func Constructor(capacity int) LRUCache {
  14. cache := LRUCache{
  15. Cap: capacity,
  16. Map: make(map[int]*Node, capacity),
  17. Head: &Node{},
  18. Last: &Node{},
  19. }
  20. cache.Head.Next = cache.Last
  21. cache.Last.Pre = cache.Head
  22. return cache
  23. }
  24. func (this *LRUCache) Get(key int) int {
  25. node, ok := this.Map[key]
  26. if !ok {
  27. return -1
  28. }
  29. this.remove(node)
  30. this.setHeader(node)
  31. return node.Val
  32. }
  33. func (this *LRUCache) Put(key int, value int) {
  34. node, ok := this.Map[key]
  35. if ok {
  36. this.remove(node)
  37. } else {
  38. if len(this.Map) == this.Cap {
  39. delete(this.Map, this.Last.Pre.Key)
  40. this.remove(this.Last.Pre)
  41. }
  42. node = &Node{Val: value, Key: key}
  43. this.Map[node.Key] = node
  44. }
  45. node.Val = value
  46. this.setHeader(node)
  47. }
  48. func (this *LRUCache) setHeader(node *Node) {
  49. this.Head.Next.Pre = node
  50. node.Next = this.Head.Next
  51. this.Head.Next = node
  52. node.Pre = this.Head
  53. }
  54. func (this *LRUCache) remove(node *Node) {
  55. node.Pre.Next = node.Next
  56. node.Next.Pre = node.Pre
  57. }
  58. /**
  59. * Your LRUCache object will be instantiated and called as such:
  60. * obj := Constructor(capacity);
  61. * param_1 := obj.Get(key);
  62. * obj.Put(key,value);
  63. */