From 8b51f36a95289bf3004ec365f294cf6c4b347eeb Mon Sep 17 00:00:00 2001 From: khoavo13 Date: Tue, 13 Aug 2024 07:40:17 +0700 Subject: [PATCH] Topic 3: Evaluate Reverse Polish Notation --- .../EvaluateReversePolishNotation.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Topic3_StackQueue/EvaluateReversePolishNotation.java diff --git a/Topic3_StackQueue/EvaluateReversePolishNotation.java b/Topic3_StackQueue/EvaluateReversePolishNotation.java new file mode 100644 index 0000000..4d7bc52 --- /dev/null +++ b/Topic3_StackQueue/EvaluateReversePolishNotation.java @@ -0,0 +1,52 @@ +import java.util.Stack; + +public class EvaluateRPN { + // Method to evaluate Reverse Polish Notation + // Time Complexity: O(N), where N is the number of tokens in the input array. + // Space Complexity: O(N),In the worst case, the stack may hold all the operands before applying any operators. + public int evalRPN(String[] tokens) { + Stack stack = new Stack<>(); // Initialize a stack to hold operands + + // Loop through each token in the input array + for (String token : tokens) { + switch (token) { + // If the token is '+', pop the top two elements, add them, and push the result + case "+": + stack.push(stack.pop() + stack.pop()); + break; + + // If the token is '-', pop the top two elements, subtract the second from the first, and push the result + case "-": + int b = stack.pop(); + int a = stack.pop(); + stack.push(a - b); + break; + + // If the token is '*', pop the top two elements, multiply them, and push the result + case "*": + stack.push(stack.pop() * stack.pop()); + break; + + // If the token is '/', pop the top two elements, divide the first by the second, and push the result + case "/": + b = stack.pop(); + a = stack.pop(); + stack.push(a / b); + break; + + // If the token is a number, parse it and push it onto the stack + default: + stack.push(Integer.parseInt(token)); + } + } + + // The result of the RPN expression is the last remaining element in the stack + return stack.pop(); + } + + public static void main(String[] args) { + EvaluateRPN evaluator = new EvaluateRPN(); + String[] tokens = {"2", "1", "+", "3", "*"}; + System.out.println(evaluator.evalRPN(tokens)); + } +} \ No newline at end of file