leetcode 中等(设计):[146, 155, 208, 211, 284, 304, 307, 341, 355, 380]
146. LRU 缓存
var LRUCache = function (capacity) {
this.capacity = capacity;
this.map = new Map();
};
LRUCache.prototype.get = function (key) {
if (this.map.has(key)) {
let value = this.map.get(key);
this.map.delete(key);
this.map.set(key, value);
return value;
}
return -1;
};
LRUCache.prototype.put = function (key, value) {
this.map.delete(key);
this.map.set(key, value);
if (this.map.size > this.capacity) {
this.map.delete(this.map.keys().next().value);
}
};
155. 最小栈
var MinStack = function () {
this.data = [];
};
MinStack.prototype.push = function (val) {
this.data.push(val);
};
MinStack.prototype.pop = function () {
this.data.pop();
};
MinStack.prototype.top = function () {
return this.data[this.data.length - 1];
};
MinStack.prototype.getMin = function () {
return Math.min(...this.data);
};
208. 实现 Trie (前缀树)
var Trie = function () {
this.data = {};
};
Trie.prototype.insert = function (word) {
this.data[word] = word;
};
Trie.prototype.search = function (word) {
return !!this.data[word];
};
Trie.prototype.startsWith = function (prefix) {
for (const key in this.data) {
if (this.data[key].startsWith(prefix)) {
return true;
}
}
return false;
};
211. 添加与搜索单词 - 数据结构设计
var WordDictionary = function () {
this.data = {};
};
WordDictionary.prototype.addWord = function (word) {
if (!this.data[word.length]) {
// 键为单词长度,值为同长度的单词集合
this.data[word.length] = [];
}
this.data[word.length].push(word);
};
WordDictionary.prototype.search = function (word) {
const len = word.length;
if (!this.data[len]) return false;
if (!word.includes(".")) return this.data[len].includes(word); // 普通字符串,直接从等长单词集合中查找即可
// 否则是正则表达式,先创建正则表达式对象
const reg = new RegExp(word);
return this.data[len].some((item) => reg.test(item));
};
284. 顶端迭代器
var PeekingIterator = function (iterator) {
this.data = [];
while (iterator.hasNext()) this.data.push(iterator.next());
this.index = 0;
this.size = this.data.length;
};
PeekingIterator.prototype.peek = function () {
return this.data[this.index];
};
PeekingIterator.prototype.next = function () {
return this.data[this.index++];
};
PeekingIterator.prototype.hasNext = function () {
return this.index < this.size;
};
304. 二维区域和检索 - 矩阵不可变
var NumMatrix = function (matrix) {
this.data = matrix;
};
NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
let sum = 0;
for (let i = row1; i <= row2; i++) {
for (let j = col1; j <= col2; j++) {
sum += this.data[i][j];
}
}
return sum;
};
307. 区域和检索 - 数组可修改
var NumArray = function (nums) {
this.data = nums;
};
NumArray.prototype.update = function (index, val) {
this.data[index] = val;
};
NumArray.prototype.sumRange = function (left, right) {
let sum = 0;
for (let i = left; i <= right; i++) {
sum += this.data[i];
}
return sum;
};
341. 扁平化嵌套列表迭代器
// 方法一:
var NestedIterator = function (nestedList) {
this.list = [];
this.traverse(nestedList);
};
NestedIterator.prototype.traverse = function (arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].isInteger()) {
this.list.push(arr[i].getInteger()); // 是Integer
} else {
this.traverse(arr[i].getList()); // 是List,递归调用
}
}
};
NestedIterator.prototype.hasNext = function () {
return this.list.length > 0;
};
NestedIterator.prototype.next = function () {
return this.list.shift();
};
// 方法二:
var NestedIterator_1 = function (nestedList) {
this.list = nestedList
.toString()
.split(",")
.filter((val) => {
return val != "";
});
};
NestedIterator_1.prototype.toString = function () {
if (this.isInteger()) {
return this.getInteger() + "";
} else {
return this.getList().toString();
}
};
NestedIterator_1.prototype.hasNext = function () {
return this.list.length > 0;
};
NestedIterator_1.prototype.next = function () {
return this.list.shift();
};
355. 设计推特
var Twitter = function () {
this.tweets = {};
this.user = {};
this.time = 0;
};
Twitter.prototype.postTweet = function (userId, tweetId) {
if (!this.tweets[userId]) {
this.tweets[userId] = [];
}
this.tweets[userId].push({ tweetId, twitTime: this.time++ });
};
Twitter.prototype.getNewsFeed = function (userId) {
let followsId = Array.from(this.user[userId] || {});
followsId.push(userId);
const allTwitters = followsId
.reduce((prev, next) => prev.concat(this.tweets[next] || []), [])
.sort((a, b) => b.twitTime - a.twitTime)
.map((item) => item.tweetId);
return Array.from(new Set(allTwitters)).slice(0, 10);
};
Twitter.prototype.follow = function (followerId, followeeId) {
if (!this.user[followerId]) {
this.user[followerId] = new Set();
}
this.user[followerId].add(followeeId);
};
Twitter.prototype.unfollow = function (followerId, followeeId) {
if (this.user[followerId]) {
this.user[followerId].delete(followeeId);
}
};
380. O(1) 时间插入、删除和获取随机元素
var RandomizedSet = function () {
this.data = new Set();
};
RandomizedSet.prototype.insert = function (val) {
if (this.data.has(val)) {
return false;
} else {
this.data.add(val);
return true;
}
};
RandomizedSet.prototype.remove = function (val) {
return this.data.delete(val);
};
RandomizedSet.prototype.getRandom = function () {
let random = Math.floor(Math.random() * this.data.size);
return Array.from(this.data)[random];
};
leetcode 中等(设计):[146, 155, 208, 211, 284, 304, 307, 341, 355, 380]的更多相关文章
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- leetcode中等题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- <Trie> 208 211
208. Implement Trie (Prefix Tree) class TrieNode{ private char val; public boolean isWord; public Tr ...
- LeetCode No.145,146,147
No.145 PostorderTraversal 二叉树的后序遍历 题目 给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 示例 输入: [1,null,2 ...
- LeetCode 中等题解(2)
31 下一个排列 Question 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须 ...
- leetcode 中等题(2)
50. Pow(x, n) (中等) double myPow(double x, int n) { ; unsigned long long p; ) { p = -n; x = / x; } el ...
- leetcode 中等题(1)
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- LeetCode 622——设计循环队列
1. 题目 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列 ...
- 领扣(LeetCode)设计哈希映射 个人题解
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
随机推荐
- zabbix使用
安装部署6.0版本 获取仓库 https://www.zabbix.com/cn/download?zabbix=6.0&os_distribution=centos&os_versi ...
- 关于.net Core在华为云的鲲鹏服务器上部署的细节纪要
由于鲲鹏使用的是ARM的cpu,,非x86的,我们公司买的是Centos,,由于需要在上面部署.net core 3.0/3.1的应用,,在按照官方的文章进行部署之后,会提示 FailFast: Co ...
- JS监听DOM创建和销毁
源码 web开发指南 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&quo ...
- 解决TrueNAS中Smb共享文件路径不区分大小写的问题
问题 在Truenas中, 默认的smb文件分享中, 文件夹是不区分大小写的. 这在一些情况下会导致无法重命名等问题, 严重时可能会造成拷贝文件时的全文件夹文件丢失. 这是linux下的情况, 在已存 ...
- 记 Codes 开源免费研发管理平台 —— 日报与工时融合集中式填报的创新实现
继上一回合生成式全局看板的创新实现后,本篇我们来讲一讲日报与工时融合集中式填报的创新实现. 市面上所有的研发管理软件,大多都有工时相关功能,但是却没有日报功能,好像也没什么问题,但是在使用过程中体验非 ...
- 详解在Linux中同时安装配置并使用 MySQL5.7 和 MySQL8.0
最近需要使用mysql8.0版本,但是原本的mysql5.7版本已经被多个服务依赖,于是想想能不能同一台服务器装多个版本的mysql,一查确实可行,这里做一个记录方便自己后期回忆 阅读本文前请注意!! ...
- The solution of P5339
problem 容斥好题,结果题解里面一堆 \(\text{NTT}\). 如果我们去掉有多少个人喜欢什么东西的条件,那么这个题就直接枚举有 \(i\) 组同学会一起讨论蔡徐坤.这一个问题十分容易. ...
- 小米 红米 Redmi MIUI14 ANDROID 系统 耗电
小米 红米 Redmi MIUI14 ANDROID 系统 耗电 在系统更新里,点右上角三点,下载完整更新包,安装好.再把电量用到关机,充电,充满开机,别拔线,继续充10分钟.我就是这么解决的,今天用 ...
- FlashDuty Changelog 2023-09-21 | 自定义字段和开发者中心
FlashDuty:一站式告警响应平台,前往此地址免费体验! 自定义字段 FlashDuty 已支持接入大部分常见的告警系统,我们将推送内容中的大部分信息放到了 Lables 进行展示.尽管如此,我们 ...
- 有点东西,template可以直接使用setup语法糖中的变量原来是因为这个
前言 我们每天写vue3代码的时候都会使用到setup语法糖,那你知道为什么setup语法糖中的顶层绑定可以在template中直接使用的呢?setup语法糖是如何编译成setup函数的呢?本文将围绕 ...