首页 > 学院 > 开发设计 > 正文

No150-Evaluate Reverse Polish Notation

2019-11-08 00:55:49
字体:
来源:转载
供稿:网友

PRoblem DescriptionAnalyseMethod one stack push and pop OnOn

Problem Description

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid Operators are +, -, *, /. Each operand may be an integer or another expression. Examples:

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

Analyse

计算逆波兰表达式求值,堆栈实现 javascript里面是push and pop操作。有一个坑就是异号除法的取整问题。如果是负数那么应该向上取整用ceil如果是正数应该向下取整。floor这一点要加个判断

Method one: stack push and pop O(n)

Javascript代码如下:

var evalRPN = function(tokens) { var arr = new Array(tokens.length); if(tokens.length == 1) return parseInt(tokens[0]); var len = tokens.length; for(var i=0;i<len;i++){ var m = tokens.shift(); if(isNum(m)){ arr.push(parseInt(m)); }else{ var b = arr.pop(); var a = arr.pop(); if(m == '*'){ arr.push(a*b); } if(m == '+') arr.push(a+b); if(m == '-') arr.push(a-b); if(m == '/') { if(a/b>0) arr.push(Math.floor(a/b)); else arr.push(Math.ceil(a/b)); } } } return arr.pop();};function isNum(str){ if(str=='+'||str =='-'|| str =='*'||str =='/') return false; else return true;}};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表