Пример #1
0
        public void Add(int key, int value)
        {
            // just need to update value and move it to the top
            if (map.ContainsKey(key))
            {
                LRUNode node = map[key];
                doubleLinkedList.RemoveNode(node);
                node.Value = value;
                doubleLinkedList.AddToTop(node);
            }
            else
            {
                // if cache is full, then remove the least recently used node
                if (count == capacity)
                {
                    LRUNode lru = doubleLinkedList.RemoveLRUNode();
                    map.Remove(lru.Key);
                    count--;
                }

                // add a new node
                LRUNode node = new LRUNode(key, value);
                doubleLinkedList.AddToTop(node);
                map[key] = node;
                count++;
            }
        }
            /// <summary>
            /// code review:
            /// I like to learn from discussion post:
            /// https://leetcode.com/problems/lru-cache/discuss/753750/C-Implementation-using-Dictionary-and-Double-Linked-List
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public void Put(int key, int value)
            {
                var update       = map.ContainsKey(key);
                var reachMaximum = map.Count == capacity;

                if (update)
                {
                    list.RemoveNode(map[key]);
                    map[key].Value = value;     // update

                    list.AddToFront(map[key]);
                }
                else
                {
                    if (reachMaximum)
                    {
                        var removed = list.RemoveLRUNode();
                        map.Remove(removed.Key);

                        // insert a new node
                        var node = new Node(key, value);
                        list.AddToFront(node);
                        map.Add(key, node);
                    }
                    else
                    {
                        // insert a new node
                        var node = new Node(key, value);
                        list.AddToFront(node);
                        map.Add(key, node);
                    }
                }
            }
            /// <summary>
            /// code review:
            /// I like to learn from discussion post:
            /// https://leetcode.com/problems/lru-cache/discuss/753750/C-Implementation-using-Dictionary-and-Double-Linked-List
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public void Put(int key, int value)
            {
                var update       = map.ContainsKey(key);
                var reachMaximum = map.Count == capacity;

                if (reachMaximum)
                {
                    if (update)
                    {
                        list.RemoveNode(map[key]);
                        map[key].Value = value; // update

                        list.AddToTail(map[key]);
                    }
                    else
                    {
                        var removed = list.RemoveLRUNode();
                        map.Remove(removed.Key);

                        // insert a new node
                        var node = new Node(key, value);
                        list.AddToTail(node);
                        map.Add(key, node);
                    }

                    return;
                }

                // two cases
                if (update)
                {
                    var removed = list.RemoveLRUNode();
                    //removed.Value = value;  // caught by Leetcode online judge
                    map[key].Value = value;

                    list.AddToTail(removed);
                }
                else
                {
                    // insert a new node
                    var node = new Node(key, value);
                    list.AddToTail(node);
                    map.Add(key, node);
                }
            }
Пример #4
0
    public void Put(int key, int value) {
        if (map.ContainsKey(key))
			{
				LRUNode node = map[key];
				doubleLinkedList.RemoveNode(node);
				node.Value = value;
				doubleLinkedList.AddToTop(node);
			}
			else
			{
				if (count==capacity) {
					LRUNode lru = doubleLinkedList.RemoveLRUNode();
					map.Remove(lru.Key);
					count--;
				}

				LRUNode node = new LRUNode(key, value);
				doubleLinkedList.AddToTop(node);
				map[key] = node;
				count++;
			}
    }