22 lines
793 B
Markdown
22 lines
793 B
Markdown
Leetcode #150 | #Medium | [Стек](../../Структуры/Стек.md) | [Математика](../../Методы/Математика.md)
|
|
## Идея
|
|
Просто стек
|
|
## Big-O
|
|
- Время ```O(N)```
|
|
- Память ```O(N)```
|
|
## Код
|
|
```Java
|
|
class Solution {
|
|
public int evalRPN(String[] tokens) {
|
|
Deque<Integer> st = new ArrayDeque<>();
|
|
for (String t : tokens) {
|
|
if (t.equals("+")) st.push(st.pop() + st.pop());
|
|
else if (t.equals("*")) st.push(st.pop() * st.pop());
|
|
else if (t.equals("-")) { int b = st.pop(), a = st.pop(); st.push(a - b); }
|
|
else if (t.equals("/")) { int b = st.pop(), a = st.pop(); st.push(a / b); }
|
|
else st.push(Integer.parseInt(t));
|
|
}
|
|
return st.pop();
|
|
}
|
|
}
|
|
``` |