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]
};
posted @ 2023-01-12 22:06  Echoyya、  阅读(21)  评论(0编辑  收藏  举报