Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import collections
- import itertools
- RE_TOKEN = re.compile('([()])|([+-])|([0-9]+)')
- class Solution:
- def calculate(self, s: str) -> int:
- stack = []
- for val in itertools.chain('(', (t.group(0) for t in RE_TOKEN.finditer(s)), ')'):
- if val == ')':
- cur = 0
- while True:
- el = stack.pop()
- if el == '(':
- stack.append(cur+pp)
- break
- elif el == '+':
- cur += pp
- elif el == '-':
- cur -= pp
- else:
- pp = int(el)
- else:
- stack.append(val)
- return stack[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement