leetcode-hard-array- 227. Basic Calculator II
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的更多相关文章
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Leetcode solution 227: Basic Calculator II
Problem Statement Implement a basic calculator to evaluate a simple expression string. The expressio ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- 224. Basic Calculator + 227. Basic Calculator II
▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...
随机推荐
- 【转载】linux SUID SGID
作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/ setuid 和 setgid 分别是 set uid ID upon execution 和 set ...
- react+antd引入 阿里图标
import iconfont from '../../../../assets/fonts/iconfont.js' const Iconfont = Icon.createFromIconfont ...
- 《设计模式之美》 <01>为什么需要学习掌握设计模式?
1. 应对面试中的设计模式相关问 题学习设计模式和算法一样,最功利.最直接的目的,可能就是应对面试了.不管你是前端工程师.后端工程师,还是全栈工程师,在求职面试中,设计模式问题是被问得频率比较高的一类 ...
- Hadoop_08_客户端向HDFS读写(上传)数据流程
1.HDFS的工作机制: HDFS集群分为两大角色:NameNode.DataNode (Secondary Namenode) NameNode负责管理整个文件系统的元数据 DataNode 负责管 ...
- 海康RTSP取流URL格式
预览取流url [海康威视]举例说明: 主码流取流: rtsp://admin:12345@192.0.0.64:554/h264/ch1/main/av_stream 子码流取流: rtsp://a ...
- 代码检查工具sonarqube介绍
SonarQube 是一款用于代码质量管理的开源工具,它主要用于管理源代码的质量.通过插件形式,可以支持众多计算机语言. 比如 java, C#, go,C/C++, PL/SQL,Cobol,Jav ...
- LoadRunner(1)
性能测试:HP LoadRunner11 一.初步概念: 1.功能测试:测试产品的功能是否满足功能需求. 如:ATM取款(在线取款)是否成功或转账操作是否成功 -- 一个用户 2.性能测试:测试产品的 ...
- Cookie/Session的机制
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- Django:报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
Django 执行迁移生成表: python manage.py migrate 报错: raise MigrationSchemaMissing("Unable to create the ...
- Easy UI 入门
Easy UI常用于企业级开发的UI和后台开发的UI,比较重. 以下组件中的加载方式,属性和事件,方法和组件种类并不全,只是作为参考和入门学习. 1.Draggable(拖动)组件 不依赖其他组件 1 ...