leetcode简单(设计):[225, 232, 303, 703, 705, 706, 933, 1603, 1656, 09, 30, 041, 03.06]
225. 用队列实现栈(先入后出)
var MyStack = function() {
this.data = []
};
MyStack.prototype.push = function(x) {
this.data.push(x)
};
MyStack.prototype.pop = function() {
return this.data.pop()
};
MyStack.prototype.top = function() {
return this.data[this.data.length - 1]
};
MyStack.prototype.empty = function() {
return !this.data.length
};
232. 用栈实现队列(先入先出)
var MyQueue = function() {
this.data = []
};
MyQueue.prototype.push = function(x) {
this.data.push(x)
};
MyQueue.prototype.pop = function() {
return this.data.shift()
};
MyQueue.prototype.peek = function() {
return this.data[0]
};
MyQueue.prototype.empty = function() {
return this.data.length == 0
};
303. 区域和检索 - 数组不可变
var NumArray = function(nums) {
this.data = nums
};
NumArray.prototype.sumRange = function(left, right) {
let sum = 0
for (let i = left; i <= right; i++) {
sum += Number(this.data[i])
}
return sum
};
703. 数据流中的第 K 大元素
var KthLargest = function(k, nums) {
this.k = k
this.data = nums
};
KthLargest.prototype.add = function(val) {
this.data.push(val)
let arr = this.data.sort((a, b) => b - a)
return arr[this.k - 1]
};
705. 设计哈希集合
var MyHashSet = function() {
this.data = []
};
MyHashSet.prototype.add = function(key) {
if (!this.contains(key)) {
this.data.push(key)
}
};
MyHashSet.prototype.remove = function(key) {
let index = this.data.indexOf(key)
if (index > -1) {
this.data.splice(index, 1)
}
};
MyHashSet.prototype.contains = function(key) {
let index = this.data.indexOf(key)
return index > -1
};
706. 设计哈希映射
var MyHashMap = function() {
this.data = {}
};
MyHashMap.prototype.put = function(key, value) {
this.data[key] = value
};
MyHashMap.prototype.get = function(key) {
return this.data[key] == undefined ? -1 : this.data[key]
};
MyHashMap.prototype.remove = function(key) {
delete this.data[key]
};var findDisappearedNumbers = function(nums) {
var res = []
for (let i = 1; i <= nums.length; i++) {
if (nums.indexOf(i) === -1) {
res.push(i)
}
}
return res
};
933. 最近的请求次数
var RecentCounter = function() {
this.data = []
};
RecentCounter.prototype.ping = function(t) {
let count = 0
this.data.push(t)
for (let i = this.data.length - 1; i >= 0; i--) {
if (this.data[i] >= (t - 3000)) {
count++
} else {
break
}
}
return count
};
1603. 设计停车系统
var ParkingSystem = function(big, medium, small) {
this.parkingCount = {
'1': big,
'2': medium,
'3': small,
}
};
ParkingSystem.prototype.addCar = function(carType) {
let park = this.parkingCount[carType]
if (park) {
this.parkingCount[carType]--
return true
}
return false
};
1656. 设计有序流
var OrderedStream = function(n) {
this.data = new Array(n + 1).fill(0)
this.ptr = 1
};
OrderedStream.prototype.insert = function(idKey, value) {
this.data[idKey] = value
if (idKey == this.ptr) {
let res = []
for (let i = this.ptr; i < this.data.length; i++) {
if (this.data[i]) {
res.push(this.data[i])
} else {
this.ptr = i
return res
}
}
return res
}
return []
};
剑指 Offer 09. 用两个栈实现队列
var CQueue = function() {
this.data = []
};
CQueue.prototype.appendTail = function(value) {
this.data.push(value)
};
CQueue.prototype.deleteHead = function() {
if (this.data.length == 0) return -1
return this.data.shift()
};
剑指 Offer 30. 包含min函数的栈
var MinStack = function() {
this.data = []
};
MinStack.prototype.push = function(x) {
this.data.push(x)
};
MinStack.prototype.pop = function() {
this.data.pop()
};
MinStack.prototype.top = function() {
return this.data[this.data.length - 1]
};
MinStack.prototype.min = function() {
return Math.min(...this.data)
};
剑指 Offer II 041. 滑动窗口的平均值
var MovingAverage = function(size) {
this.data = []
this.size = size
};
MovingAverage.prototype.next = function(val) {
this.data.push(val)
let limit = Math.min(this.size, this.data.length)
let num = 0
for (let i = 0; i < limit; i++) {
num += this.data[this.data.length - 1 - i]
}
return num / limit
};
面试题 03.06. 动物收容所
var AnimalShelf = function() {
this.animalList = []
this.numList = []
};
AnimalShelf.prototype.enqueue = function(animal) {
this.numList.push(animal[0])
this.animalList.push(animal[1])
};
AnimalShelf.prototype.dequeueAny = function() {
if (this.animalList.length > 0) {
let res = [this.numList[0], this.animalList[0]]
this.animalList.shift()
this.numList.shift()
return res
}
return [-1, -1]
};
AnimalShelf.prototype.dequeueDog = function() {
let index = this.animalList.indexOf(1)
if (index > -1) {
let res = [this.numList[index], 1]
this.animalList.splice(index, 1)
this.numList.splice(index, 1)
return res
}
return [-1, -1]
};
AnimalShelf.prototype.dequeueCat = function() {
let index = this.animalList.indexOf(0)
if (index > -1) {
let res = [this.numList[index], 0]
this.animalList.splice(index, 1)
this.numList.splice(index, 1)
return res
}
return [-1, -1]
};
leetcode简单(设计):[225, 232, 303, 703, 705, 706, 933, 1603, 1656, 09, 30, 041, 03.06]的更多相关文章
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- Java消息系统简单设计与实现
前言:由于导师在我的毕设项目里加了消息系统(本来想水水就过的..),没办法...来稍微研究研究吧..简单简单... 需求分析 我的毕设是一个博客系统,类似于简书这样的,所以消息系统也类似,在用户的消息 ...
- 学生与部门管理app-产品功能与界面的简单设计
学生与部门管理app-产品功能与界面的简单设计 1. 结对成员学号 我:********* 大佬:*******10 2. 需求分析(NABCD模型) 2.1 N-需求 各个部门在开学初占据学校青春广 ...
- C#网络编程TCP通信实例程序简单设计
C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...
- Java秒杀简单设计二:数据库表和Dao层设计
Java秒杀简单设计二:数据库表Dao层设计 上一篇中搭建springboot项目环境和设计数据库表 https://www.cnblogs.com/taiguyiba/p/9791431.html ...
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...
- 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- leetcode简单题6
今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- LC滤波器简单设计法 - 一文读懂LC滤波器简单设计方法及原理介绍,LC值计算方法
LC滤波器概述 LC滤波器也称为无源滤波器,是传统的谐波补偿装置.LC滤波器之所以称为无源滤波器,顾名思义,就是该装置不需要额外提供电源.LC滤波器一般是由滤波电容器.电抗器和电阻器适当组合而成,与谐 ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
随机推荐
- 各大插件市场智能助手评分榜出炉!百度Comate稳居第一
近日,在VSCode.Jetbrains等各大插件市场智能助手评分榜中,百度Comate分别以4.5和4.4位列第一,通义灵码位居第二.第三,CodeGeeX.iFlyCode.aiXcoder.Gi ...
- golang select 和外层的 for 搭配
select语句通常与for循环搭配使用,但并不是必须的. 在某些情况下,select可能会直接放在一个独立的goroutine中,没有外层的for循环. 这通常发生在你知道只会有一次或有限次操作的情 ...
- MindSponge分子动力学模拟——体系控制(2024.05)
技术背景 在传统的分子动力学模拟软件中,对于分子体系的控制,例如控制体系的相对位置亦或是绝对位置,通常都是通过施加一些约束算法来实现的.例如用于限制化学键的LINCS算法,又比如水分子体系非常常用的S ...
- u8二次开发再遇问题再总结
在调用api生成采购入库单时,报错:该操作会造成订单到货和入库同时存在,请重新检查操作!普通采购必有订单,存货[0501-0304-0075]不能手工录入 这是因为:采购入库单单据必须要录入上游单据, ...
- 一文搞懂 Spring Bean 的生命周期
一. 前言 在学习Spring框架的IOC.AOP两大功能之前,首先需要了解这两个技术的基础--Bean.在Spring框架中,Bean无处不在,IOC容器管理的对象就是各种各样的Bean.理解Bea ...
- Python 爬虫神器 requests 工具
一.模块安装 pip install requests 二.常用方法 在实际的爬虫中,其实真正用到的只有 GET.POST,像其他的方法基本用不到,比如:DELETE.HEAD.PUT 等. 1.GE ...
- Nginx 调试模块 echo-nginx-module
引言 Nginx 作为一个高性能的 HTTP 和反向代理 Web 服务器.如今很多项目都会选择 Nginx 作为反向代理服务器,但是避免不了在使用的过程中,会遇到各种各样的问题.因此 echo-ngi ...
- HTML——超链接标签
一.超链接标签的基本使用 超链接是浏览者和服务器的交互的主要手段,也叫超级链接或a链接,是网页中指向一个目标的连接关系,这个目标可以是网页.网页中的具体位置.图片.邮件地址.文件.应用程序等. 超链接 ...
- C#使用WebView2替代Electron
C#想要实现Electron那样混合桌面程序可以用以下几个库.本文使用EdgeSharp NanUIgithub.com/NetDimension/NanUI Photinogithub.com/ ...
- LLM 大模型学习必知必会系列(十):基于AgentFabric实现交互式智能体应用,Agent实战
LLM 大模型学习必知必会系列(十):基于AgentFabric实现交互式智能体应用,Agent实战 0.前言 **Modelscope **是一个交互式智能体应用基于ModelScope-Agent ...