티스토리 뷰
Problem
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
- The valid operators are '+', '-', '*', and '/'.
 - Each operand may be an integer or another expression.
 - The division between two integers always truncates toward zero.
 - There will not be any division by zero.
 - The input represents a valid arithmetic expression in a reverse polish notation.
 - The answer and all the intermediate calculations can be represented in a 32-bit integer.
 
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
- 1 <= tokens.length <= 10^4
 - tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
 
Solution
후위 표기법으로 된 배열이 주어지면 계산한 결과를 반환하는 문제입니다.
package io.lcalmsky.leetcode.evaluate_reverse_polish_notation;
import java.util.Stack;
public class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int a, b;
        for (String token : tokens) {
            switch (token) {
                case "+":
                    stack.push(stack.pop() + stack.pop());
                    break;
                case "-":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b - a);
                    break;
                case "*":
                    stack.push(stack.pop() * stack.pop());
                    break;
                case "/":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b / a);
                    break;
                default:
                    stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
각 토큰을 순차적으로 탐색하면서 연산자가 아닐 땐 stack에 넣고, 연산자일 경우 해당 연산자에 맞게 stack에서 정수를 꺼내 연산한 뒤 연산한 결과를 stack에 다시 저장하는 것을 반복하면 최종적으로 하나의 요소만 남게 됩니다.
Test
package io.lcalmsky.leetcode.evaluate_reverse_polish_notation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
    @Test
    void testAll() {
        assertAll(
                () -> test(new String[]{"2", "1", "+", "3", "*"}, 9),
                () -> test(new String[]{"4", "13", "5", "/", "+"}, 6),
                () -> test(new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}, 22)
        );
    }
    private void test(String[] tokens, int expected) {
        // when
        Solution solution = new Solution();
        int actual = solution.evalRPN(tokens);
        // then
        assertEquals(expected, actual);
    }
}'Algorithm > LeetCode' 카테고리의 다른 글
| [LeetCode] 103. Binary Tree Zigzag Level Order Traversal (0) | 2023.07.20 | 
|---|---|
| [LeetCode] 22. Generate Parentheses (1) | 2023.07.19 | 
| [LeetCode] 380. Insert Delete GetRandom O(1) (1) | 2023.07.17 | 
| [LeetCode] 238. Product of Array Except Self (0) | 2023.07.16 | 
| [LeetCode] 34. Find First and Last Position of Element in Sorted Array (0) | 2023.07.15 | 
                           댓글
                               
                           
                           
                           
                       
                   
                                        공지사항
                                        
                                
                            
                                
                                
                                    최근에 올라온 글
                                    
                            
                                
                                
                                    최근에 달린 댓글
                                    
                            
                                
                                - Total
 
- Today
 
- Yesterday
 
                                    링크
                                    
                                
                            
                                
                                
                                    TAG
                                    
                            
                                
                                - 알고리즘
 - 스프링 부트 튜토리얼
 - Spring Boot JPA
 - QueryDSL
 - 헥사고날 아키텍처
 - 클린 아키텍처
 - Linux
 - Java
 - JPA
 - JSON
 - r
 - 함께 자라기 후기
 - gRPC
 - 스프링 부트 회원 가입
 - proto3
 - Spring Data JPA
 - 스프링 데이터 jpa
 - 스프링 부트
 - intellij
 - spring boot app
 - 스프링 부트 애플리케이션
 - Spring Boot
 - spring boot application
 - Jackson
 - 스프링부트
 - 함께 자라기
 - spring boot jwt
 - Spring Boot Tutorial
 - leetcode
 - @ManyToOne
 
| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                                    글 보관함
                                    
                            
                    