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 ...
随机推荐
- Java基础 韩顺平老师的 常用类 的部分笔记
459,八大Wrapper类 包装类的分类 1) 针对八种基本数据类型相应的引用类型-包装类 2) 有了类的特点, 就可以调用类中的方法. 460,装箱和拆箱 package com.hspedu.W ...
- Machine Learning - 笔记1
一.监督学习(supervised learning) 1.回归(regression) ①按我的理解来说,就是对于训练模型所用到的数据集,能够知道Input和Output的特征.比如 ...
- PHP-FPM 运行原理分析
概述 PHP-FPM 是一种 Master / Worker 多进程运行模式,进程的数量可以通过 php-fpm.conf 进行具体的配置. Master 进程主要负责 CGI.PHP 环境的初始化. ...
- HTML——input之复选框
在 HTML 中,把 <input> 标签中的 type 属性设置为 checkbox 可以实现多选框的效果.具体语法格式如下: <input type="checkbox ...
- linux server Vue 或其它单页面项目站点 nginx 实施部署
# nginx vue 处理前台路由 history 模式刷新 404 的问题 location / { try_files $uri $uri/ /index.html; if ($uri ~* . ...
- ubuntu安装之后要做的10件事
部分内容整理自网络,如果侵权还请联系 基础配置 换源 换源 [ubuntu清华源镜像站] ctrl+click,进入镜像站链接,选择合适的版本,将镜像地址粘贴到本地文件里,对于: <24.04的 ...
- mobile select 移动端下拉框
官方链接 原生 js 移动端选择控件,不依赖任何库 可传入普通数组或者 json 数组 可根据传入的参数长度,自动渲染出对应的列数,支持单项到多项选择 自动识别是否级联 选择成功后,提供自定义回调函数 ...
- windows报错
如果说你dns没有权威的话1.先去long.com上面右键属性把"区域传送给所有服务器打勾"2.右键属性,在名称分析器中,输入要添加为辅助dns的服务器的ip显示解析成功就可以了
- Python中多线程的简单使用
from threading import Timer import time def run1(): print(1) print(2) print(3) def run2(): print(&qu ...
- [ROI 2018] Innophone 题解
[ROI 2018] Innophone 看了半天网上仅有的一篇题解--才堪堪写出来 不过在LOJ上看提交,全是 KTT,看得我瑟瑟发抖(不会 题意翻译 在平面上有一些点,你需要在这个平面上任意确定一 ...