Leetcode之Basic Calculator & Basic Calculator II
2016-04-04 21:26
369 查看
相对而言,Basic Calculator II更简单一点,所以我们先从Basic Calculator II开始。
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
实现用字符串表示的表达式求值,只包含非负数、加减乘除和空格。
这道题目的直观感受就是我们需要使用栈来实现,且由于乘除的优先级高于加减,所以当遇到乘除时我们需要立即进行计算,当遇到加减时先计算前一个运算符操作的结果,再保存此运算符,不断循环。
自己写的代码很冗长,这里贴出从别人写的帖子借鉴的思路写出来的代码,不需要用到栈,很巧妙:
Basic Calculator:
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open
the plus
and empty spaces .
You may assume that the given expression is always valid.
Some examples:
实现包含括号、非负数与加减的表达式求值,与前一题的思路很类似,只是这里因为括号的存在需要用到栈,代码如下:
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,
+,
-,
*,
/operators
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
实现用字符串表示的表达式求值,只包含非负数、加减乘除和空格。
这道题目的直观感受就是我们需要使用栈来实现,且由于乘除的优先级高于加减,所以当遇到乘除时我们需要立即进行计算,当遇到加减时先计算前一个运算符操作的结果,再保存此运算符,不断循环。
自己写的代码很冗长,这里贴出从别人写的帖子借鉴的思路写出来的代码,不需要用到栈,很巧妙:
class Solution(object): def calculate(self, s): """ :type s: str :rtype: int """ s = re.sub(r'\d+', ' \g<0> ', s) ops = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div} func = ops['+'] expr = s.split() total = pos = factor1 = 0 func = ops['+'] while pos < len(expr): element = expr[pos] if element in '+-': total = func(total, factor1) func = ops[element] elif element in '*/': pos += 1 factor2 = int(expr[pos]) factor1 = ops[element](factor1, factor2) else: factor1 = int(expr[pos]) pos += 1 return func(total, factor1)很是精简,这里我们需要做些初始化,使第一个操作数为0,第一个操作符为加号。
Basic Calculator:
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open
(and closing parentheses
),
the plus
+or minus sign
-, non-negative integers
and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
实现包含括号、非负数与加减的表达式求值,与前一题的思路很类似,只是这里因为括号的存在需要用到栈,代码如下:
class Solution(object): def calculate(self, s): """ :type s: str :rtype: int """ ops = {'+': operator.add, '-': operator.sub} operator_stack = [] value_stack = [] length = len(s) pos = 0 value_stack.append(0) operator_stack.append('+') while pos < length: if s[pos] == ' ': pos += 1 continue elif s[pos] == '(': operator_stack.append(s[pos]) value_stack.append(0) operator_stack.append('+') elif s[pos].isdigit(): factor = 0 while pos < length and s[pos].isdigit(): factor = factor * 10 + int(s[pos]) pos += 1 pos -= 1 value_stack.append(factor) elif s[pos] in '+-': factor2 = value_stack.pop() factor1 = value_stack.pop() factor = ops[operator_stack.pop()](factor1, factor2) value_stack.append(factor) operator_stack.append(s[pos]) elif s[pos] == ')': factor2 = value_stack.pop() factor1 = value_stack.pop() factor = ops[operator_stack.pop()](factor1, factor2) operator_stack.pop() value_stack.append(factor) pos += 1 return ops[operator_stack.pop()](value_stack[0], value_stack[1])
相关文章推荐
- 断言
- jvm参数在哪里设置
- poj_1007
- Java 导入包正确依旧报错的处理方法
- linux 下redis php扩展安装、redis服务安装
- hdu1418(欧拉公式)
- jquery 设置元素内容html(),text(),val()
- 第五周 项目一 三角形类雏形(3)
- 大型项目使用Automake/Autoconf完成编译配置
- 网络七层协议
- 判断一串字符中是否有回文串
- 编译原理课程作业2
- 如何用Eclipse查看Hadoop源码
- 区间覆盖问题
- Java反射---获取field信息跟构造函数信息
- 子对象的派生类构造函数以及变量的输入
- Linux安装memcache
- 将单向链表按某值划分成左边小,中间相等,右边大
- jvm(13)-线程安全与锁优化(转)
- C++时间和日期