# leetcode之Basic Calculator II

class Solution(object):
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
import re
listofnum = []
listofsym = []
symbol = ['*', '/', '+', '-']
#空集
if len(s) == 0:
return 0
#一个字母
try:
return int(s)
except:
#将所有项分别列出来
s = re.sub(' ', '', s)
n = ''
for i in range(len(s)):
try:
if s[i] in symbol:
listofnum.append(int(n))
listofsym.append(s[i])
n = ''
elif i == len(s) - 1:
n = n + s[i]
listofnum.append(int(n))
else:
n = n + s[i]
except:
listofsym.append(s[i])
#计算乘除
if s[i] in ['+', '-']:
a = listofsym.pop()
if listofsym != []:
if listofsym[-1] in ['*', '/']:
listofnum1 = []
listofsym1 = []
listofnum1.append(listofnum.pop())
try:
while listofsym[-1] not in ['+', '-']:
listofsym1.append(listofsym.pop())
listofnum1.append(listofnum.pop())
except:
listofnum1 = listofnum1
listofsym1 = listofsym1
for i in listofsym1[::-1]:
a1 = listofnum1.pop()
b1 = listofnum1.pop()
if i == '*':
listofnum1.append(a1 * b1)
else:
if a1 > 0:
if b1 > 0:
listofnum1.append(a1 / b1)
else:
listofnum1.append(-(a1 / (-b1)))
else:
if b1 > 0:
listofnum1.append(-(-a1 / b1))
else:
listofnum1.append(a1 / b1)
listofnum.append(listofnum1[0])
listofsym.append(a)
#计算最后的
if listofsym[-1] in ['*', '/']:
listofnum1 = []
listofsym1 = []
listofnum1.append(listofnum.pop())
while listofsym != []:
if listofsym[-1] not in ['+', '-']:
listofsym1.append(listofsym.pop())
listofnum1.append(listofnum.pop())
else:
break
for i in listofsym1[::-1]:
a1 = listofnum1.pop()
b1 = listofnum1.pop()
if i == '*':
listofnum1.append(a1 * b1)
else:
if a1 > 0:
if b1 > 0:
listofnum1.append(a1 / b1)
else:
listofnum1.append(-(a1 / (-b1)))
else:
if b1 > 0:
listofnum1.append(-(-a1 / b1))
else:
listofnum1.append(a1 / b1)
listofnum.append(listofnum1[0])
while len(listofsym) != 0:
a = listofnum.pop()
b = listofnum.pop()
try:
if listofsym[-1] == '-':
if listofsym[-2] == '-':
listofnum.append(a + b)
else:
listofnum.append(b - a)
else:
if listofsym[-2] == '-':
listofnum.append(b - a)
else:
listofnum.append(b + a)
listofsym.pop()
except:
if listofsym[-1] == '-':
return b - a
else:
return a + b
return listofnum[0]

• 本文已收录于以下专栏：

## 【LeetCode】Basic Calculator && Basic Calculator II

1、Basic Calculator  Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution  ...
• u013027996
• 2015年06月24日 11:01
• 3518

## LeetCode[227] Basic Calculator II

• hust_dxxxd
• 2016年05月04日 11:29
• 377

## LeetCode 227. Basic Calculator II 解题报告【python】

• kuaisuzhuceh
• 2016年01月30日 14:11
• 606

## leetcode 227: Basic Calculator II

leetcode 227: Basic Calculator II PYTHON JAVA C++
• xudli
• 2015年06月26日 03:18
• 6896

## LeetCode-227. Basic Calculator II (JAVA)实现计算器 II

LeetCode-227. Basic Calculator II (JAVA)实现计算器 II
• mine_song
• 2017年04月30日 09:54
• 447

## Leetcode 224: Basic Calculator

Leetcode 224: Basic Calculator java c++ python
• xudli
• 2015年06月19日 00:59
• 7580

## 【LeetCode】Basic Calculator 解题报告

【题目】 Implement a basic calculator to evaluate a simple expression string. The expression str...
• ljiabin
• 2015年06月14日 11:19
• 8278

## LeetCode 224 Basic Calculator (后缀表达式 栈)

LeetCode 224 Basic Calculator (后缀表达式 栈)
• Tc_To_Top
• 2016年08月28日 23:45
• 855

## LeetCode 770. Basic Calculator IV题目解析

• cqx5555
• 2018年01月26日 12:47
• 145

## Leetcode之Basic Calculator & Basic Calculator II

Leetcode之basic calculator、basic calculator II
• blueskyliulan
• 2016年04月04日 21:26
• 203

举报原因： 您举报文章：leetcode之Basic Calculator II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)