[알고리즘] LeetCode - Evaluate Reverse Polish Notation

2023. 8. 31. 03:07·알고리즘/leet code
 

Evaluate Reverse Polish Notation - LeetCode

Can you solve this real interview question? Evaluate Reverse Polish Notation - You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation [http://en.wikipedia.org/wiki/Reverse_Polish_notation]. Evaluate t

leetcode.com

문제 설명

  • 역 폴란트 표기법이라고 하지만 쉽게 말해 후위 표기법이다.
  • 후위 표기법으로 주어진 식이 문자형 배열로 주어진다.
  • 후위표기법으로 표기된 식을 계산한 결과 값을 반환해야 한다.
  • 유효한 연산자는 +, -, *, / 가 있다.
  • 두 정수 사이의 나눗셈에서 소숫점은 버린다.

문제 해결

  • token값이 숫자가 아니면 stack에서 2개의 값을 pop한 후 주어진 연산 기호로 계산 후 다시 stack에 push한다.
  • token값이 숫자라면 그대로 stack에 push한다.
  • 반복문을 다 돌고 마지막에 stack에 남은 값이 최종 연산 값이므로 그대로 반환한다.

시간 복잡도

  • O(N)

풀이 코드

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < tokens.length; i++) {
            String now = tokens[i];

            if (now.equals("+") || now.equals("-") || now.equals("*") || now.equals("/")) {
                int B = stack.pop();
                int A = stack.pop();
                stack.push(getResult(A, B, now));
            } else {
                stack.push(Integer.parseInt(now));
            }
        }

        return stack.pop();
    }

    int getResult(int A, int B, String now) {
        switch(now) {
            case "+": 
                return A + B;
            case "-":
                return A - B;
            case "*":
                return A * B;
            default:
                return A / B;
        }
    }
}

결과

시간 메모리
6 ms 42.9 MB

 

'알고리즘 > leet code' 카테고리의 다른 글

[알고리즘] LeetCode - Contains Duplicate II  (0) 2023.08.31
[알고리즘] LeetCode - Two Sum  (0) 2023.08.31
[알고리즘] LeetCode - Min Stack  (0) 2023.08.31
[알고리즘] LeetCode - Linked List Cycle  (0) 2023.08.28
[알고리즘] LeetCode - Longest Substring Without Repeating Characters  (0) 2023.08.28
'알고리즘/leet code' 카테고리의 다른 글
  • [알고리즘] LeetCode - Contains Duplicate II
  • [알고리즘] LeetCode - Two Sum
  • [알고리즘] LeetCode - Min Stack
  • [알고리즘] LeetCode - Linked List Cycle
tableMinPark
tableMinPark
Backend Engineer
  • tableMinPark
    Sangmin's Tech Blog
    tableMinPark
  • 전체
    오늘
    어제
  • 블로그 메뉴

    • tech study blog
    • my github
    • monglife github
    • 분류 전체보기 (48)
      • 개발 (7)
        • java (0)
        • kotlin (0)
        • spring boot (0)
        • android (0)
        • junit5 (5)
        • architecture (2)
      • 데브옵스 (3)
        • docker (3)
        • github action (0)
        • grafana (0)
        • prometheus (0)
        • elk (0)
      • 알고리즘 (35)
        • baek joon (0)
        • programmers (4)
        • leet code (29)
      • 일상 (3)
        • Wear OS 앱 개발기 (2)
        • 회고 (1)
  • 태그

    ubuntu
    volume
    mongs
    layered architecture
    Kotlin
    Android
    monglife
    synchronized
    apple watch
    Container
    wear os
    Thread
    java
    Galaxy watch
    20.04
    micro service
    docker-compose
    jetpack-compose
    MVVM
    docker
    MSA
    bind mount
    레이어드 아키텍처
    docker network
    docker compose
  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
tableMinPark
[알고리즘] LeetCode - Evaluate Reverse Polish Notation
상단으로

티스토리툴바