LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class LRUCache {
 public:
  explicit LRUCache(int capacity) : n(capacity) {}

  int get(int key) {
    if (n == 0 || !m.count(key)) {
      return -1;
    }
    q.remove(key);         // 从队列中删除该key
    q.emplace_front(key);  // 再添加到队首
    return m[key];
  }

  void put(int key, int value) {
    if (n == 0) {
      return;
    }
    if (m.count(key)) {      // 已存在该key
      m[key] = value;        // 修改值
      q.remove(key);         // 从队列中删除该key
      q.emplace_front(key);  // 再添加到队首
      return;
    }
    if (size(m) == n) {    // 不存在该 key 且到达容量上限
      m.erase(q.back());   // 移除该元素
      q.pop_back();        // 移除队尾的key
    }                      // 容量重新到达未满状态
    q.emplace_front(key);  // 添加key到队首
    m.emplace(key, value);
  }

 private:
  int n;
  list<int> q;
  unordered_map<int, int> m;
};
class LRUCache {
 public:
  explicit LRUCache(int capacity) : n(capacity) {}

  int get(int key) {
    if (n == 0 || !m.count(key)) {
      return -1;
    }
    q.splice(begin(q), q, m[key]);  // 提到队首
    return q.front().second;
  }

  void put(int key, int value) {
    if (n == 0) {  // 已存在该key
      return;
    }
    if (m.count(key)) {
      q.splice(begin(q), q, m[key]);  // 提到队首
      q.front().second = value;       // 修改值
      return;
    }
    if (size(m) == n) {  // 不存在该 key 且到达容量上限
      m.erase(q.back().first);
      q.pop_back();
    }  // 容量重新到达未满状态
    q.emplace_front(key, value);
    m.emplace(key, begin(q));
  }

 private:
  int n;
  list<pair<int, int>> q;  // 仍要保存 key,在哈希表中移除时要知道队尾元素的 key
  unordered_map<int, list<pair<int, int>>::iterator> m;
};