算法| 栈 队列

文章介绍了如何用栈实现队列,用队列实现栈,以及涉及的有效括号检查、删除字符串重复字符、逆波兰表达式求值和找出前k个高频元素等IT技术问题的解决方案。
摘要由CSDN通过智能技术生成
    1. 用栈实现队列
    1. 用队列实现栈
    1. 有效的括号
    1. 删除字符串中的所有相邻重复项
    1. 逆波兰表达式求值
    1. 前 K 个高频元素

232. 用栈实现队列


var MyQueue = function () {
    this.stackIn = []
    this.stackOut = []
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function (x) {
    this.stackIn.push(x)
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function () {
    const size = this.stackOut.length
    if (size) {
        return this.stackOut.pop()
    }
    while (this.stackIn.length) {
        this.stackOut.push(this.stackIn.pop())
    }
    return this.stackOut.pop()

};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function () {
    const x = this.pop()
    this.stackOut.push(x)
    return x
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function () {
    return !this.stackIn.length && !this.stackOut.length
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

225. 用队列实现栈


var MyStack = function () {
    this.queue1 = []
    this.queue2 = []
};

/** 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function (x) {
    this.queue1.push(x)
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function () {
    if (!this.queue1.length) {
        [this.queue1, this.queue2] = [this.queue2, this.queue1]
    }
    while (this.queue1.length>1) {
        this.queue2.push(this.queue1.shift())
    }
    return this.queue1.shift()
};

/**
 * @return {number}
 */
MyStack.prototype.top = function () {
    const x = this.pop()
    this.queue1.push(x)
    return x
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function () {
    return !this.queue1.length && !this.queue2.length
};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */

20. 有效的括号

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
    const stack = [];
    let left = "({[";
    let right = ")}]";
    for (let ch of s) {
        if (left.includes(ch)) {
            stack.push(ch);
        }
        if (right.includes(ch)) {
            let top = stack[stack.length - 1];
            if (top === undefined) return false;
            if (
                (ch === "}" && top === "{") ||
                (ch === ")" && top === "(") ||
                (ch === "]" && top === "[")
            ) {
                stack.pop();
            }else {
                return false
            }
        }
    }
    return stack.length ===0 
};

1047. 删除字符串中的所有相邻重复项

/**
 * @param {string} s
 * @return {string}
 */
var removeDuplicates = function(s) {
    const stack = []
    for(let ch of s){
        if(stack.length){
            top = stack[stack.length-1]
            if(top===ch){
                stack.pop()
            }else{
                stack.push(ch)
            }
        }else{
            stack.push(ch)
        }
        
    }
    return stack.join('')
};

150. 逆波兰表达式求值

/**
 * @param {string[]} tokens
 * @return {number}
 */
var evalRPN = function (tokens) {
    const s = new Map([
        ["+", (a, b) => a * 1 + b * 1],
        ["-", (a, b) => b - a],
        ["*", (a, b) => b * a],
        ["/", (a, b) => (b / a) | 0]
    ]);
    const stack = [];
    for (const i of tokens) {
        if (!s.has(i)) {
            stack.push(i);
            continue;
        }
        stack.push(s.get(i)(stack.pop(), stack.pop()))
    }
    return stack.pop();
};

347. 前 K 个高频元素

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function (nums, k) {
    const map = {};
    for (let ch of nums) {
        map[ch] === undefined ? (map[ch] = 1) : (map[ch] += 1);
    }
    let arr = Object.entries(map);
    //   注意排序
    arr.sort((a, b) => b[1] - a[1]);
    let ans = [];
    for (let i = 0; i < k; i++) {
        ans.push(arr[i][0]);
    }
    return ans;
};
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值