mycode  29.58%

class Solution(object):
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
def deal(data,flag):
data[:] = data[::-1]
while data:
if len(data) == 1:
break
a = data.pop()
if a == '+':
b = data.pop()
c = last + b
data.append(c)
elif a == '-':
b = data.pop()
c = last - b
data.append(c)
else:
last = a
return data[0] data = []
s = s.strip()
tokens = ['*','/','+','-']
l , r = 0, 0
for i in s:
if not i : continue
if i not in tokens:
r += 1
else:
data.append(int(s[l:r]))
r += 1
l = r
data.append(i)
data.append(int(s[l:r])) if '*' not in data and "/" not in data:
return deal(data,0) res = []
data[:] = data[::-1]
while data:
if '*' not in data and "/" not in data:
break
a = data.pop()
if a == '*':
b = data.pop()
res.pop()
c = last*b
data.append(c)
elif a == '/':
b = data.pop()
res.pop()
if b == 0:
return None
c = last // b
data.append(c)
else:
last = a
res.append(a)
return deal(res + data[::-1],1)

参考

import math
class Solution(object): def apply_pending_op(self, stack, pending_op, cur_int):
if pending_op is None:
stack.append(cur_int)
elif pending_op == '-':
stack.append(-cur_int)
elif pending_op == '+':
stack.append(cur_int)
elif pending_op == '*':
left = stack.pop()
right = cur_int
stack.append(left * right)
elif pending_op == '/':
left = stack.pop()
right = cur_int
# bypasses integer division rounding toward negative infinity
quo = int(float(left) / right)
stack.append(int(quo))
else:
raise ValueError(pending_op) def calculate(self, s):
"""
:type s: str
:rtype: int
"""
cur_int = 0
stack = []
pending_op = None
for c in s:
if c.isdigit():
cur_int = cur_int * 10 + int(c)
elif c in ('*', '/', '+', '-'):
self.apply_pending_op(stack, pending_op, cur_int)
cur_int, pending_op = 0, c self.apply_pending_op(stack, pending_op, cur_int) return sum(stack)

leetcode-hard-array- 227. Basic Calculator II的更多相关文章

  1. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

  3. 【LeetCode】227. Basic Calculator II

    Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...

  4. [LeetCode] 227. Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Leetcode solution 227: Basic Calculator II

    Problem Statement Implement a basic calculator to evaluate a simple expression string. The expressio ...

  6. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  7. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  8. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  9. (medium)LeetCode 227.Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  10. 224. Basic Calculator + 227. Basic Calculator II

    ▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...

随机推荐

  1. 进程、线程、协程的基本解析(python代码)

    进程什么是进程?程序就是一堆放在磁盘上的代码,进程是一段程序的运行过程正规点说,进程一般由程序.数据集.进程控制块三部分组成 什么进程切换?进程切换是,一个正在运行的进程被中断,操作系统指定另一个进程 ...

  2. Java反射【三、方法的反射】

    获取一个类下的所有方法 可以获取类类型后,获取到所有方法及相关信息 Method[] ms = c.getMethods(); 获取方法列表(public) Method[] ms = c.getDe ...

  3. Delphi 使用Tabel组件的记录查找

    樊伟胜

  4. 最终章·MySQL从入门到高可用架构报错解决

    1. 报错原因:MySQL的socket文件目录不存在. 解决方法:创建MySQL的socket文件目录 mkdir /application/mysql-5.6.38/tmp 2. 报错原因:soc ...

  5. 【转】awk学习笔记

    Awk学习笔记 整理:Jims of 肥肥世家 <jims.yang@gmail.com> Copyright © 2004 本文遵从GPL协议,欢迎转载.修改.散布. 第一次发布时间:2 ...

  6. 详解python中的生成器表达式

    什么是生成器表达式 还记得列表解析吗?我们把[]换成()就变成生成器表达式了. g = (x for x in [1, 2, 3, 4]) print(g) # <generator objec ...

  7. Oracle【子查询】

    Oracle子查询:当一个查询依赖于另外一个查询的结果的时候,就需要使用子查询.单行子查询 :筛选条件不明确,需要执行一次查询且查询结果只有一个字段且字段值只有一个.注意:where子句中允许出现查询 ...

  8. c++ mfc和win32项目

    win32项目是一个底层的窗口的实现过程,它采用的库仅仅是windows.h,我们通过winain作为函数的入口,然后经过窗口类的内容的填写,窗口的注册,创建,显示刷新,到最后的消息循环,这是一个wi ...

  9. pytest的使用

    一.python安装 1.windows(server): 双击python-3.6.7-amd64.exe执行安装流程,使用默认安装方式即可. 安装完成后查看是否安装成功: C:\Users\Adm ...

  10. STM32/EMC/ZILIAO

    https://www.st.com/content/ccc/resource/technical/document/application_note/a2/9c/07/d9/2a/b2/47/dc/ ...