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. mybatis sql语句中 in() 长度为0或null的情况

    mybatis sql语句中 in() 长度为0或null的情况 比如: select * from A where colName IN <foreach collection="m ...

  2. Win10应用商店缓存信息多如何去清理?

    Win10系统的应用商店相比之前有了许多的更新,微软也成立了专门的团队准备对应用商店进行完善,但是我们在使用应用商店的过程中会产生许多缓存文件,占用电脑空间资源,也会影响电脑的运行速度. 下面好系统重 ...

  3. DataGrip导出查询结果数据

    1 按钮 2 选择保存位置即可

  4. Linux awk命令 --三剑客老大

    Linux awk命令 --三剑客老大 基本用法: awk  [参数]  ['找谁{干啥}']  文件 参数: -F 分隔符 -v 创建或修改awk变量 OFS 输出分割符 awk显示每一列的时候分隔 ...

  5. python函数:装饰器、修正、语法糖、有参装饰器、global与nonlocal

    一.装饰器 二.装饰器修正1 三.装饰器修正2 四.装饰器的语法糖 五.有参.无参装饰器 六.global与nonlocal 一.装饰器 ''' 1 什么是装饰器 器=>工具 装饰=>指的 ...

  6. Ubuntu 18.04 系统 Update 后内核升级到5.0 导致 VMware Workstation 不正常工作

    Ubuntu 18.04 系统升级后,发现内核从 4.18 升级到 5.0,从而导致 VMware Workstation 启动主动更新,但总会显示 vmmon 和 vmnet 俩模块编译失败. 感谢 ...

  7. 网络协议相关面试问题-DNS相关面试问题

    对于网络上的大部通讯都是基于TCP/IP协议的, 其中最重要的是IP协议,它是基于IP地址的,而计算机通讯只能识别IP地址,如192.168.0.1,而不能识别像咱们在浏览器敲得见名之义的" ...

  8. 关于layui表格渲染templet解析单元格的问题

    原文链接:https://blog.csdn.net/wyp_comeon/article/details/81735951关于表格解析自定义单元格的解析参数请先详细查看官方文档:http://www ...

  9. 阿里云端安装mysql

    首先查看系统版本,是64位的centos7 file /sbin/init 安装指南如下 https://www.cnblogs.com/thinkingandworkinghard/p/671125 ...

  10. 小程序+tgit

    1.微信公众平台-设置-开发者工具  开通 腾讯云和tgit权限管理 如果遇到问题 ..用户二次验证什么的   直接去 腾讯云-安全设置 将“敏感操作”和“异地登陆” 中的保护去掉 2.微信公众平台- ...