150.逆波兰表达式求值(栈M)

/**
 * @param {string[]} tokens
 * @return {number}
 */
 var evalRPN = function(tokens) {
    let stack = []
    let set = new Set(['+','-','*','/'])
    for(let i = 0;i<tokens.length;i++){
        if(!set.has(tokens[i])){
            stack.push(parseInt(tokens[i]))
            continue
        }
        let num0 = parseInt(stack.pop())
        let num1 = parseInt(stack.pop())
        switch(tokens[i]){
            case '+':
                stack.push(num1 + num0)
                break
            case '-':
                stack.push(num1 - num0)
                break
            case '*':
                stack.push(num1 * num0)
                break
            case '/':
                stack.push(num1 / num0)
                break
        }
    }
    return parseInt(stack.pop())
}

 

上一篇:学习笔记(12):150讲轻松搞定Python网络爬虫-requests库-发送POST请求


下一篇:【HarmonyOS挑战赛】150份定制礼品,20张现场门票,等你来!