File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn eval_rpn ( tokens : Vec < String > ) -> i32 {
3+ let mut stack: Vec < i32 > = Vec :: new ( ) ;
4+
5+ for token in tokens {
6+ match & token[ ..] {
7+ "+" => {
8+ let second_operand = stack. pop ( ) . unwrap ( ) ;
9+ let first_operand = stack. pop ( ) . unwrap ( ) ;
10+ stack. push ( first_operand + second_operand)
11+ }
12+ "-" => {
13+ let second_operand = stack. pop ( ) . unwrap ( ) ;
14+ let first_operand = stack. pop ( ) . unwrap ( ) ;
15+ stack. push ( first_operand - second_operand)
16+ }
17+ "*" => {
18+ let second_operand = stack. pop ( ) . unwrap ( ) ;
19+ let first_operand = stack. pop ( ) . unwrap ( ) ;
20+ stack. push ( first_operand * second_operand)
21+ }
22+ "/" => {
23+ let second_operand = stack. pop ( ) . unwrap ( ) ;
24+ let first_operand = stack. pop ( ) . unwrap ( ) ;
25+ stack. push ( first_operand / second_operand)
26+ }
27+ value => stack. push ( value. parse :: < i32 > ( ) . unwrap ( ) ) ,
28+ }
29+ }
30+
31+ stack[ 0 ]
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments