//雙鏈表節點struct
dlistnode
};class
lrucache
void removenode(dlistnode*node)
void ajustnodetohead(dlistnode*newnode)
intget(int
key)
return -1
; }
void put(int key, int
value)
else
dlistnode* newnode = new
dlistnode(key, value);
ajustnodetohead(newnode);
_map.insert(make_pair(key, newnode));}}
private
: dlistnode*head;
dlistnode*tail;
unordered_map
_map;
int_size;
};
leetcode上的題目,使用最優的時間複雜度,用hashmap儲存指向雙鏈表的指標實現查詢o(1),而雙鏈表的刪除也是o(1),put和get元素都是o(1),hashmap+雙鏈表的方法真的神,一開始自己用vector和list雖然也實現了功能,但是資料就慘很多,原因是list的查詢是o(n)的,還需要定義乙個指定大小的vector,著實扣腳,下邊給出這個版本供參考:
classlrucache
intget(int
key)
}return
target;
}void put(int key, int
value) }}
if(_list.size()==_size)
_list.push_back(pair
(key,value));
++_vec[key];
}private
: list
int,int>>_list;
//記錄每個key是否存在
vector_vec;
//鍊錶最大長度
int_size;
};
先進先出淘汰演算法
c語言實現作業系統最佳淘汰演算法 include include define length 20 定義陣列長度 int bwolist length 淘汰序列 void initlist int arr int bwo int init,int arr if flag 0 if temflag 1...
佇列 先進先出
看前面的是什麼型別 指標 普通 出現指標指向空報錯的情況下,傳參不能為空,可以傳個空間的位址給他 queue front next null 從尾進 先進先出 define crt secure no warnings pragma once include include include incl...
智慧型出庫,先進先出
3分鐘左右 t erp location 庫存表 4000行 如果是40000呢 code time location number 2000 20180101 a1 1 10 2000 20170102 b1 1 20 2000 20180103 c1 1 5 2000 20180101 d1 1...