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 ...
随机推荐
- 【题解】A18537.我心中珍藏的游戏
题目跳转 思路: 题目问最多可以获得的额外伤害,其实就是询问在这些技能中,如何怎样选取一个最优的发动技能顺序使得攻击加成最大.我们可以把每一个技能看作成一个图的顶点,把每一个攻击加成看作图的边,权制为 ...
- synchronized锁升级过程
更过博文请关注:https://blog.bigcoder.cn JDK 1.6后锁的状态总共有四种,级别由低到高依次为:无锁.偏向锁.轻量级锁.重量级锁,这四种锁状态分别代表什么,为什么会有锁升级? ...
- WPF之单例模式
项目 2019/10/09 问题 2019年10月9日星期三 上午2:46 1.为了实现单例模式,在App类中添加了如下代码,使用了信号量,但是为什么返回;isNew一直为true public ...
- 一个前后端都有的后台管理系统,使用nest.js和vue3
今天介绍一个新的Vue后台管理框架,相比其他后台功能丰富管理系统,这个后台管理系统可以用干净简洁来形容--Nova-admin Nova-admin Nova-admin 是一个基于Vue3.Vite ...
- 前端项目npm install安装报错:code ERESOLVE ERESOLVE could not resolve
背景:使用npm 安装依赖的时候,发现报了如下的错误: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tr ...
- Qt OPC UA通信
介绍 OPC UA全称Open Platform Unified Architecture,开放平台统一架构,是工业自动化领域通用的数据交换协议,它有两套主要的通信机制:1.客户端-服务器通信:2.发 ...
- LeetCode 682. Baseball Game 棒球比赛(C++/Java)
题目: You're now a baseball game point recorder. Given a list of strings, each string can be one of th ...
- work06
练习题:=============================================================第七题: 1.定义方法 isSXH(int num) 功能:判断数字n ...
- 深入了解 C# Span:高性能内存操作的利器
深入了解 C# Span:高性能内存操作的利器 在 C# 7.2 中引入的 Span<T> 类型为我们提供了一种高效且安全地对内存进行操作的方式.Span<T> 是一个轻量级的 ...
- MYSQL8-快速生成表结构(用于生成文档)
各种工具都有,没有特别趁手的.不如自己用sql处理. SELECT column_name AS CODE, CASE WHEN column_comment IS NULL OR TRIM(colu ...