LC 146:LRU 缓存
题目 题目链接 请你设计并实现一个满足 LRU (最近最少使用) 缓存约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 void put(int key, in...
题目 题目链接 请你设计并实现一个满足 LRU (最近最少使用) 缓存约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 void put(int key, in...
题目 题目链接 给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度。 示例 1: 输入: s = “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 示例 2: 输入: s = “bbbbb” 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 示例 3: 输入: ...
Comparator 接口 实现 Comparator 接口 一般使用场景为用 Collections.sort() 或 Arrays.sort() 进行自定义排序。需要实现 compare(T first, T second) 方法: public interface Comparator<T> { int compare(T first, int second); }...
力扣中的许多题都涉及求最大公约数 (Greatest Common Divisor, GCD) 和最小公倍数 (Least Common Multiple, LCM)。本文记录了 GCD 与 LCM 的代码实现。 GCD 可以使用辗转相除法求解两个数的最大公约数。 递归版 class GCD { public int gcd(int a, int b) { i...
Basics Searching Big Search Spaces Search space: \(\{0, 1\}^n\). $2^n$ possible candidates. Assume \(\mathbf{a}^*\in \{0, 1\}^n\) is the goal vector. Size (formally called the card...
Perplexity (PPL) Perplexity is defined as the exponentiated average negative log-likelihood of a sequence. If we have a tokenized sequence \(X=(x_0, x_1, \cdots, x_t)\), then the perplexity of \(X\...
问题背景 推导 答案为 \(\Theta(N)\)。 时间复杂度为: [\begin{align} &\sum_{k = 0}^{\log N - 1} 2^k \left(\log N - k \right) =& \sum_{k = 0}^{\log N - 1} 2^k\cdot \log N - \sum_{k = 0}^{\log N - 1} k\cdo...
Introduction Search VS Recommendation Search engines Recommender systems User has an information need User has an interest User typ...
Tabular Value Based Methods Backup Monte-Carlo backup: zero bias, high variance. [\begin{align} V(S_t) \leftarrow V(S_t) + \alpha(G_t - V(S_t)) \end{align}] Temporal difference backup: hig...
《C++ Primer》读书笔记。 基本作用 const 对象创建后,值不能再改变。 初始化: int i = 42; const int ci = i; // 正确:i 的值被拷贝给了 ci int j = ci; // 正确: ci 的值被拷贝给了 j 默认情况下,const 对象仅在文件内有效。 需要文件间共享时,使用 extern 关键字。 ...