← Home

150. Evaluate Reverse Polish Notation

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
stackC++Markdown
150

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 150. Evaluate Reverse Polish Notation, the solution in this repository is mainly a stack solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: stack.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are is_number, evalRPN.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 16 ms, faster than 95.61% of C++ online submissions for Evaluate Reverse Polish Notation.
02//Memory Usage: 12.2 MB, less than 13.10% of C++ online submissions for Evaluate Reverse Polish Notation.
03class Solution {
04public:
05    bool is_number(const std::string& s){
06        if(s.empty()) return false;
07        if(s.size() > 1 && s[0] == '-' && s.find_first_not_of("0123456789", 1) == std::string::npos) return true;
08        return s.find_first_not_of("0123456789") == std::string::npos;
09    };
10    
11    int evalRPN(vector<string>& tokens) {
12        stack<int> oprs;
13        
14        for(const string& token : tokens){
15            if(is_number(token)){
16                oprs.push(stoi(token));
17            }else{
18                int b = oprs.top(); oprs.pop();
19                int a = oprs.top(); oprs.pop();
20                
21                int res;
22                switch(token[0]){
23                    case '+':
24                        res = a+b;
25                        break;
26                    case '-':
27                        res = a-b;
28                        break;
29                    case '*':
30                        res = a*b;
31                        break;
32                    case '/':
33                        res = a/b;
34                        break;
35                }
36                
37                oprs.push(res);
38                
39                // cout << a << token << b << " = " << res << endl;
40            }
41        }
42        
43        return oprs.top();
44    }
45};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.