LRU和最长前缀和(计算机网络算法)

1. 最长公共前缀 LeetCode 14

最优时间:\(O(mn)\),m 字符串平均长度,n 字符串数量;空间\(O(1)\),原地处理 思路:拿第一个字符串当基准,逐个字符比对其余所有字符串,遇到不匹配 / 越界直接截断返回。

cpp 复制代码
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";
        // 以第一个串为基准
        for(int i = 0; i < strs[0].size(); ++i){
            char c = strs[0][i];
            for(int j = 1; j < strs.size(); ++j){
                // 当前串长度不够i 或者字符不等,直接截取前缀返回
                if(i >= strs[j].size() || strs[j][i] != c){
                    return strs[0].substr(0,i);
                }
            }
        }
        return strs[0];
    }
};

2.LRU 缓存 LeetCode 146

最优解法:哈希表 + 双向链表

  • get/put 均摊 \(O(1)\)
  • std::list 双向链表;unordered_map 保存 key 到链表迭代器映射
  • list 头部:最近使用;尾部:最久未使用,满容量删除尾部
cpp 复制代码
#include <unordered_map>
#include <list>
using namespace std;

class LRUCache {
private:
    int cap;
    // key,value
    list<pair<int,int>> l;
    // key -> list迭代器
    unordered_map<int, list<pair<int,int>>::iterator> mp;

public:
    LRUCache(int capacity) {
        cap = capacity;
    }

    int get(int key) {
        auto it = mp.find(key);
        if(it == mp.end()){
            return -1;
        }
        // 取出节点,移动到链表头部(标记最近访问)
        auto node_it = it->second;
        int val = node_it->second;
        l.erase(node_it);
        l.push_front({key,val});
        mp[key] = l.begin();
        return val;
    }

    void put(int key, int value) {
        auto it = mp.find(key);
        // 1.key已经存在:删除旧节点,头部插入新值
        if(it != mp.end()){
            l.erase(it->second);
        }else{
            // 2.不存在,容量满,删掉最久未使用(链表尾部)
            if(l.size() >= cap){
                auto last = l.back();
                mp.erase(last.first);
                l.pop_back();
            }
        }
        l.push_front({key,value});
        mp[key] = l.begin();
    }
};
相关推荐
郝学胜-神的一滴35 分钟前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
玩转单片机与嵌入式1 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?
网络·人工智能·算法
Nazarick3 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
16月6日-晴3 小时前
斐波那契数列题解
算法·leetcode
郝学胜-神的一滴3 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
Keven_115 小时前
算法札记:有限差分
算法
忙里偷闲学python5 小时前
GK2A 林火蔓延预报算法说明
算法
我是伪码农5 小时前
11.21程序设计
数据结构·算法
Tisfy6 小时前
LeetCode 3561.移除相邻字符:栈(相邻相消)
算法·leetcode·字符串·题解·栈·相邻相消
战6176 小时前
Python 基础算法:三种排序、二分查找与递归
开发语言·python·算法