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]的更多相关文章

  1. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  2. Java消息系统简单设计与实现

    前言:由于导师在我的毕设项目里加了消息系统(本来想水水就过的..),没办法...来稍微研究研究吧..简单简单... 需求分析 我的毕设是一个博客系统,类似于简书这样的,所以消息系统也类似,在用户的消息 ...

  3. 学生与部门管理app-产品功能与界面的简单设计

    学生与部门管理app-产品功能与界面的简单设计 1. 结对成员学号 我:********* 大佬:*******10 2. 需求分析(NABCD模型) 2.1 N-需求 各个部门在开学初占据学校青春广 ...

  4. C#网络编程TCP通信实例程序简单设计

    C#网络编程TCP通信实例程序简单设计 采用自带 TcpClient和TcpListener设计一个Tcp通信的例子 只实现了TCP通信 通信程序截图: 压力测试服务端截图: 俩个客户端链接服务端测试 ...

  5. Java秒杀简单设计二:数据库表和Dao层设计

    Java秒杀简单设计二:数据库表Dao层设计 上一篇中搭建springboot项目环境和设计数据库表  https://www.cnblogs.com/taiguyiba/p/9791431.html ...

  6. SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建

    SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...

  7. 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  8. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  9. LC滤波器简单设计法 - 一文读懂LC滤波器简单设计方法及原理介绍,LC值计算方法

    LC滤波器概述 LC滤波器也称为无源滤波器,是传统的谐波补偿装置.LC滤波器之所以称为无源滤波器,顾名思义,就是该装置不需要额外提供电源.LC滤波器一般是由滤波电容器.电抗器和电阻器适当组合而成,与谐 ...

  10. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

随机推荐

  1. .NET快速实现网页数据抓取

    前言 今天我们来讲讲如何使用.NET开源(MIT License)的轻量.灵活.高性能.跨平台的分布式网络爬虫框架DotnetSpider来快速实现网页数据抓取功能. 注意:为了自身安全请在国家法律允 ...

  2. 彻底搞懂JavaScript原型和原型链

    基于原型编程 在面向对象的编程语言中,类和对象的关系是铸模和铸件的关系,对象总是从类创建而来,比如Java中,必须先创建类再基于类实例化对象. 而在基于原型编程的思想中,类并不是必须的,对象都是通过克 ...

  3. Django 视图views的基本使用

    在 Django 中,视图函数是一个 Python 函数或者类,开发者主要通过编写视图函数来实现业务逻辑.视图函数首先接受来自浏览器或者客户端的请求,并最终返回响应,视图函数返回的响应可以是 HTML ...

  4. C# wpf之控制屏幕显示方向旋转

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...

  5. 002. git 分支管理

    git分支 git分支,从本质上来讲仅仅是指向提交对象的可变指针,在这一点上与svn是有着本质区别,svn的分支实际就是个目录而已. git默认分支名字是 master,在多次提交操作后,你其实已经有 ...

  6. FLV 分析脚本

    一.需求 通过脚本,可以检查本地flv文件格式是否正确,可以打印每个Tag中的二进制内容 二.效果 可以看到VideoTag中开始处增加了一段SEI数据,并且可以看到部分字段,gameid.time. ...

  7. 关于@synchronized

    一.结论 1)@synchronized内部使用的是recursive_mutex_lock,也就是递归锁,对于统一线程来说,@synchronized加锁的方法可以重复加锁. 比如代码: - (vo ...

  8. Java常用的三个方法 `wait ` `notify` `notifyAll`

    常用的三个方法 wait notify notifyAll wait();方法使当前线程进入等待状态,直到另一个线程调用该对象的notify()或notifyAll()方法来唤醒它 notify(); ...

  9. (数据科学学习手札161)高性能数据分析利器DuckDB在Python中的使用

    本文完整代码及附件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在几天前,经过六年多的 ...

  10. INFINI Labs 产品更新 | Easysearch 优化字段压缩提升写入速度,Console 优化数据迁移和校验等功能

    INFINI Labs 产品又更新啦~.本次更新概要如下:Easysearch 增强 source_reuse 压缩功能,并大幅提升写入速度:Console 优化了数据迁移和校验功能,新增了通用的数据 ...