课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Solution: Evaluate reverse polish notation

Solution: Evaluate reverse polish notation - Java教程

课程: Java Algorithms

Solution: Evaluate reverse polish notation

- [Instructor] Before going into the implementation, let's talk about these helper methods given to us to use. The first is isNumber. This one takes in a token that is a string and returns true or false based on whether it can be parsed into a double. The second is isOperator. It takes in a string and returns whether or not it matches one of the basic operations. That's plus for addition, minus for subtraction and et cetera. The last function we're given is performOperation. Given the operator and both operands, it will perform the operation. If the operator is invalid, it'll throw an exception. We can use these helper methods in our implementation in order to keep our function code more concise. Let's implement evaluateRPN. We know that each operand comes before its operator. This means we'll need to keep track of each number until we see the operator. We can do this with a stack. We also know that each operand and…

内容