add postfix solns
This commit is contained in:
parent
658601137a
commit
e91751a4ce
39
0150_evaluate-reverse-polish-notation/README.md
Normal file
39
0150_evaluate-reverse-polish-notation/README.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
|
||||||
|
|
||||||
|
Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression.
|
||||||
|
|
||||||
|
**Note** that division between two integers should truncate toward zero.
|
||||||
|
|
||||||
|
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
|
||||||
|
|
||||||
|
**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 <= 104`
|
||||||
|
* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.
|
24
0150_evaluate-reverse-polish-notation/python3/solution.py
Normal file
24
0150_evaluate-reverse-polish-notation/python3/solution.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import math
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def evalRPN(self, tokens: List[str]) -> int:
|
||||||
|
compute = {
|
||||||
|
'+': lambda a, b: a + b,
|
||||||
|
'-': lambda a, b: a - b,
|
||||||
|
'*': lambda a, b: a * b,
|
||||||
|
'/': lambda a, b: math.trunc(a / b)
|
||||||
|
}
|
||||||
|
|
||||||
|
stack = deque()
|
||||||
|
|
||||||
|
for token in tokens:
|
||||||
|
if token.lstrip('-').isdigit():
|
||||||
|
stack.append(int(token))
|
||||||
|
else:
|
||||||
|
right = stack.pop()
|
||||||
|
left = stack.pop()
|
||||||
|
|
||||||
|
stack.append(compute[token](left, right))
|
||||||
|
|
||||||
|
return stack[-1]
|
17
lintcode/271-prefix-notation-to-postfix-notation/README.md
Normal file
17
lintcode/271-prefix-notation-to-postfix-notation/README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
Given a string array representing an expression, and return the Reverse Polish notation of this expression. (remove the parentheses)
|
||||||
|
|
||||||
|
Definition of _Reverse Polish Notation_:
|
||||||
|
|
||||||
|
* [https://en.wikipedia.org/wiki/Reverse\_Polish\_notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
|
||||||
|
|
||||||
|
Example
|
||||||
|
|
||||||
|
Example 1:
|
||||||
|
|
||||||
|
Input: ["3", "-", "4", "+", "5"] Output: ["3", "4", "-", "5", "+"] Explanation: 3 - 4 + 5 = -1 + 5 = 4 3 4 - 5 + = -1 5 + = 4
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Example 2:
|
||||||
|
|
||||||
|
Input: ["(", "5", "-", "6", ")", "*", "7"] Output: ["5","6","-","7","*"] Explanation: (5 - 6) * 7 = -1 * 7 = -7 5 6 - 7 * = -1 7 * = -7
|
@ -0,0 +1,49 @@
|
|||||||
|
from typing import (
|
||||||
|
List,
|
||||||
|
)
|
||||||
|
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
"""
|
||||||
|
@param expression: A string array
|
||||||
|
@return: The Reverse Polish notation of this expression
|
||||||
|
"""
|
||||||
|
def convert_to_r_p_n(self, expression: List[str]) -> List[str]:
|
||||||
|
op_priority = {
|
||||||
|
'/': 1 << 4,
|
||||||
|
'*': 1 << 4,
|
||||||
|
'+': 1 << 2,
|
||||||
|
'-': 1 << 2,
|
||||||
|
'(': 1 << 0
|
||||||
|
}
|
||||||
|
|
||||||
|
op_stack = deque()
|
||||||
|
result = []
|
||||||
|
|
||||||
|
for token in expression:
|
||||||
|
if token.isdigit():
|
||||||
|
result.append(token)
|
||||||
|
|
||||||
|
elif token == '(':
|
||||||
|
op_stack.append(token)
|
||||||
|
|
||||||
|
elif token == ')':
|
||||||
|
while len(op_stack) > 0 and op_stack[-1] != '(':
|
||||||
|
result.append(op_stack.pop())
|
||||||
|
|
||||||
|
op_stack.pop()
|
||||||
|
|
||||||
|
else:
|
||||||
|
# For any other operator, keep popping and appending to results as long as there
|
||||||
|
# are higher priority operators in the stack
|
||||||
|
while len(op_stack) > 0 and op_priority[op_stack[-1]] >= op_priority[token]:
|
||||||
|
result.append(op_stack.pop())
|
||||||
|
|
||||||
|
op_stack.append(token)
|
||||||
|
|
||||||
|
# Pop and append the remaining operators in the stack
|
||||||
|
while len(op_stack) > 0:
|
||||||
|
result.append(op_stack.pop())
|
||||||
|
|
||||||
|
return result
|
Loading…
Reference in New Issue
Block a user