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 ...
随机推荐
- 安装anaconda3卡在Unpacking payload ...
ananconda3在centos7虚拟机上,直接进行ananconda3安装但是始终卡在Unpacking payload ..., 虚拟机的核心数调到2或者2以上即可解决
- Linux安装ElastSearch
Linux安装ES 准备好Linux系统,软件安装前需要对当前系统做一些优化配置 系统配置修改 一.内存优化 在/etc/sysctl.conf添加如下内容: fs.file-max=655360 系 ...
- Sqlserver存储过程中使用try-catch和事务
BEGIN TRY BEGIN TRANSACTION --逻辑代码 COMMIT TRANSACTION --提交事务 END TRY BEGIN CATCH SELECT @Msg = ERROR ...
- NOIP模拟53
我在时光斑驳深处,聆听到花开的声音. 前言 这套题好像是随便拼接起来的,来自三套不同的题,最后一道还是学长出的(nb 场上为数不多的几次死磕一道题正解,大概有三个小时吧(惭愧,前两个小时看错题了,一直 ...
- const与指针的组合
① const int *p; //指向一个整型常量的指针,p可变,p指向的对象不可变. ② int const *p; //同上. ③ int * const p; //p不可变,p指向的对象可变( ...
- 知乎x-zse-96逆向分析
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 目标网站 aHR0cHM6 ...
- 使用itextPDF实现PDF电子公章工具类
使用itextPDF实现PDF电子公章工具类 一.制作公章 在线网站:印章生成器 - Kalvin在线工具 (kalvinbg.cn) 然后对公章进行下载保存 盖章图片: 二.生成数字签名 2.1: ...
- 解决:Maven PKIX path building failed: sun.security.provider.certpath
在构建SpringBoot项目时,maven下载依赖会报 PKIX path building failed: sun.security.provider.certpath的错误. 使用https:/ ...
- LeetCode 208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)(C++/Java)
题目: Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); t ...
- linux Centos8系统,防火墙配置常用命令,systemctl 和firewall
本文环境:Linux系统CentOS 8.2 64bit CentOS 7版本及以上版本较centos 6有较大改动,例如:采用systemctl命令来开启service,它是服务管理中主要的工具,融 ...