Re: [閒聊] 每日LeetCode

作者: JerryChungYC (JerryChung)   2024-01-30 20:14:24
https://leetcode.com/problems/evaluate-reverse-polish-notation
150. Evaluate Reverse Polish Notation
回傳逆波蘭表示法的結果值
有效運算子為 "+" "-" "*" "/"
每個操作數會是整數或是另一個表達式
兩個整數之間的除法是向零截斷
不會有被零除的情況
答案和中間計算都可以用32位元整數表示
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
看了一下維基 就是遇到數字 -> 存起來
遇到運算 -> 取最後2個數來計算
Python3 code:
作者: JerryChungYC (JerryChung)   2024-01-30 20:15:00
啊 打字打太久 還先洗了個碗反過來用 if token in ["+", "-", "*", "/"] 也可以

Links booklink

Contact Us: admin [ a t ] ucptt.com