파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 12. 17. 23:45

https://leetcode.com/problems/evaluate-reverse-polish-notation/

 

Evaluate Reverse Polish Notation - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

스택을 이용하는거 까진 알겠는데
생각보다 간단했다는건 몰랐음 =_=;

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        st = []
        for token in tokens:
            if token in ['*', '/', '-', '+']:
                r = st.pop()
                l = st.pop()
                st.append(str(int(eval(l+token+r))))
            else:
                st.append(token)
        return int(st[-1])
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.12.19
Today's Challenge  (0) 2022.12.18
Today's Challenge  (0) 2022.12.16
Today's Challenge  (0) 2022.12.15
Today's Challenge  (0) 2022.12.14
Comments