-
Notifications
You must be signed in to change notification settings - Fork 0
/
227.py
39 lines (36 loc) · 992 Bytes
/
227.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 227. Basic Calculator II
# https://leetcode.com/problems/basic-calculator-ii/
# Python solution
import math
class Solution:
def calculate(self, s: str) -> int:
self.s = s.replace(' ', '')
if self.s.isdigit():
return int(s)
mem = []
n = 0
sign = '+'
for i in range(len(self.s)):
if self.s[i].isdigit():
n = n*10 + int(self.s[i])
else:
self.calc(n, sign, mem)
sign = self.s[i][:]
n = 0
self.calc(n, sign, mem)
return sum(mem)
@staticmethod
def calc(n, sign, mem):
if sign == '+':
mem.append(n)
elif sign == '-':
mem.append(-n)
elif sign == '*':
mem.append(mem.pop() * n)
else:
m = mem.pop()
if m // n < 0:
mem.append(math.ceil(m/n))
else:
mem.append(m // n)
return mem