后缀表达式求值
/** * * @param tokens string字符串一维数组 * @return int整型 */ function evalRPN( tokens ) { // write code here let stack = [] for(let x of tokens){ if(x === '+'){ let num1 = stack.pop(); let num2 = stack.pop(); stack.push(num1+num2) }else if(x === '-'){ let num1 = stack.pop(); let num2 = stack.pop(); stack.push(num2-num1); }else if(x === '*'){ let num1 = stack.pop(); let num2 = stack.pop(); stack.push(num1*num2) }else if(x === '/'){ let num1 = stack.pop(); let num2 = stack.pop(); let num = num2/num1 stack.push(parseInt(num)) }else{ stack.push(parseInt(x)) } } return stack[0] } module.exports = { evalRPN : evalRPN };
栈和队列算法 文章被收录于专栏
栈和队列算法