-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicCalculatorII.py
More file actions
31 lines (29 loc) · 964 Bytes
/
basicCalculatorII.py
File metadata and controls
31 lines (29 loc) · 964 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/basic-calculator-ii/
# Author: Miao Zhang
# Date: 2021-01-28
class Solution:
def calculate(self, s: str) -> int:
stack = []
num = 0
pre_op = '+'
for i, c in enumerate(s):
if c.isdigit():
num = 10 * num + int(c)
if i == len(s) - 1 or c in '+-*/':
if pre_op == '+':
stack.append(num)
elif pre_op == '-':
stack.append(-num)
elif pre_op == '*':
stack.append(stack.pop() * num)
elif pre_op == '/':
top = stack.pop()
if top < 0:
stack.append((-top // num) * -1)
else:
stack.append(top // num)
pre_op = c
num = 0
return sum(stack)