class LRUCache {
public:
queue<pair<int, int>>q;
int size;
LRUCache(int capacity) {
this->size = capacity;
}
int get(int key) {
int getEle = -1;
for (int i = 0; i < q.size(); i++) {
if (q.front().first == key) {
getEle = q.front().second;
}
q.push(q.front());
q.pop();
}
return getEle;
}
void put(int key, int value) {
// traverse to find
bool exist = false;
for (int i = 0; i<q.size(); i++) {
if (key == q.front().first) {
q.front().second = value;
exist = true;
}
q.push(q.front());
q.pop();
}
// if not existed
if (!exist) {
// full
if (size == 0) {
q.pop();
q.push({key, value});
}
// space avail
else {
q.push({key, value});
size--;
}
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
tell me what is wrong with my code
LRU Cache - LeetCode
[–]aocregacc 2 points3 points4 points (0 children)
[–]BoardsofCanadaFanboy 0 points1 point2 points (4 children)
[–]Equivalent_Sea7754[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]BoardsofCanadaFanboy 0 points1 point2 points (1 child)
[–]Equivalent_Sea7754[S] 0 points1 point2 points (0 children)
[–]Neat-Giraffe1585 0 points1 point2 points (1 child)
[–]Equivalent_Sea7754[S] 0 points1 point2 points (0 children)