Leetcode 224/227/772 计算器
题目描述 Leetcode 224
Leetcode 224:
这里想让我们实现一个基础的计算器,来计算给定的字符串。
给定的字符串中包含 ( ) + - 和非负整数和空格。
# Example 1:
#
# Input: "1 + 1"
# Output: 2
# Example 2:
#
# Input: " 2-1 + 2 "
# Output: 3
# Example 3:
#
# Input: "(1+(4+5+2)-3)+(6+8)"
# Output: 23
注意:
- 我们可以认为给定的表达式总是有效的。
- 不要使用内置的 eval 库函数。
题目分析 Leetcode 224
刚开始做这道题时,确定了使用栈作为数据结构,但在字符串入栈时会面临两个问题:
- 输入的数字是连续的问题:比如 "123+123" 这里就需要将 123 转换成 int 再压入栈。
- 计算的顺序问题:如果按照正常的顺序入栈,在进行减法运算时就会发生问题,比如 "(7-8+9)"
- 正常的入栈结果是(/7/-/8/+/9
- 而出栈的结果是 9+8-7 与期待的结果不符。
解决思路,通过一个变量 operand 累计需要转换的操作数,将字符串反序来保证正常的计算结构。
# 224. 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
# Example 1:
#
# Input: "1 + 1"
# Output: 2
# Example 2:
#
# Input: " 2-1 + 2 "
# Output: 3
# Example 3:
#
# Input: "(1+(4+5+2)-3)+(6+8)"
# Output: 23
# Note:
# You may assume that the given expression is always valid.
# Do not use the eval built-in library function.
class Solution(object):
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
stack = []
operand, n = 0, 1
# Reverse string
for index in range(len(s)-1, -1, -1):
char = s[index]
if char == " ":
continue
if char.isdigit():
# transform number string to number
# like "123" to 3+20+100=123
operand = operand + int(char) * n
n = n * 10
else:
# put before formatted string to the stack
if n != 1:
stack.append(operand)
n, operand = 1, 0
# due to the reversed order, '(' represents the end
if char == '(':
self.eval(stack)
# put current char to the stack like '+', '+', '-'
else:
stack.append(char)
if n != 1:
stack.append(operand)
return self.eval(stack)
def eval(self, stack):
result = stack.pop()
while stack:
sign = stack.pop()
if sign == "+":
operand = stack.pop()
result = result + operand
elif sign == "-":
operand = stack.pop()
result = result - operand
elif sign == ")":
break
stack.append(result)
return result
题目描述 Leetcode 227
这次规定的字符串包含了 * / 符号,但缺少了 ( ) 的优先级。
# Input: "3+2*2"
# Output: 7
# Example 2:
# Input: " 3/2 "
# Output: 1
# Example 3:
# Input: " 3+5 / 2 "
# Output: 5
题目分析 Leetcode 227
这次换了一个解决思路,可以把给定的字符串中加减运算都看成相加运算,当遇到 - 时,入栈的结果是当前操作数的相反数。当遇到乘法和除法时,将栈顶元素取出进行运算后再放回栈顶。
具体解决时,可以使用 pre_sign 作为变量保存当前数组的正负,比如 "5-6" 可以看成 "+5" + "-6",加减表示当前符合的正负。
class Solution1:
def calculate(self, s):
index = 0
stack = []
operand = 0
pre_sign = "+"
while index < len(s):
char = s[index]
if char == "":
continue
if char.isdigit():
operand = 10 * operand + int(char)
if char in ['+', '-', '*', '/'] or index == len(s)-1:
if pre_sign == '+':
stack.append(operand)
elif pre_sign == "-":
stack.append(-operand)
elif pre_sign == "*":
stack.append(stack.pop() * operand)
elif pre_sign == "/":
stack.append(int(stack.pop() / operand))
pre_sign = char
operand = 0
index += 1
return sum(stack)
题目描述 Leetcode 772
在上题的基础上增加了 ( ) 的运算。
# "1 + 1" = 2
# " 6-4 / 2 " = 4
# "2*(5+5*2)/3+(6/2+8)" = 21
# "(2+6* 3+5- (3*14/7+2)*5)+3"=-12
题目分析 Leetcode 772
这题其实上上两题的综合,由于在括号内的计算和外部一致,所以这里考虑使用递归来实现,将括号内计算的结果放入到栈中。但这里需要考虑到一个小问题,就是在进行递归运算时,由于括号内的字符串已经被计算过了,所以需要返回括号内的字符串的长度,用于忽略这些字符的计算。
class Solution2:
def calculate(self, s):
index = 0
stack = []
operand = 0
pre_sign = "+"
while index < len(s):
char = s[index]
if char == "":
continue
if char.isdigit():
operand = 10 * operand + int(char)
if char in ['+', '-', '*', '/', '(', ')'] or index == len(s)-1:
if char == "(":
operand, lenth = self.calculate(s[index+1:])
index = index + lenth
if pre_sign == '+':
stack.append(operand)
elif pre_sign == "-":
stack.append(-operand)
elif pre_sign == "*":
stack.append(stack.pop() * operand)
elif pre_sign == "/":
stack.append(int(stack.pop() / operand))
if char == ")":
return sum(stack), index+1
pre_sign = char
operand = 0
index += 1
return sum(stack)
Leetcode 224/227/772 计算器的更多相关文章
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- Java实现 LeetCode 227 基本计算器 II(二)
227. 基本计算器 II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 . 整数除法仅保留整数部分. 示例 1: 输入: ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- [LeetCode] 227. 基本计算器 II
题目链接: https://leetcode-cn.com/problems/basic-calculator-ii 难度:中等 通过率:33.2% 题目描述: 实现一个基本的计算器来计算一个简单的字 ...
- Java实现 LeetCode 224 基本计算器
224. 基本计算器 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 . 示例 1: 输入: "1 ...
- Leetcode 227.基本计算器II
基本计算器II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 . 整数除法仅保留整数部分. 示例 1: 输入: " ...
- Leetcode 224.基本计算器
基本计算器 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 . 示例 1: 输入: "1 + 1 ...
随机推荐
- [access]第一篇-平台
[access]第一篇-平台 上海盟威软件有限公司http://www.accessoft.com/ 下载地址http://www.accessgood.com/ 中山市天鸣科技发展有限公司http: ...
- 使用MockMVC与Junit进行单体测试
1.pom.xml追加 junit spring-test 2.测试共通类 @ContextConfiguration(locations = { "classpath:springfram ...
- P1850 换教室——期望DP
题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 2n2n2n 节课程安排在 nnn 个时间段上.在第 iii(1≤i≤n1 \leq i ...
- Mac 解决终端:-bash: /Users/xxx/.profile: No such file or directory
touch ~/.profile加入export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 参考:https://www.zhihu.com/ ...
- C# 下划线转驼峰
/// <summary> /// 转换 /// </summary> /// <param name="sender"></param& ...
- A·F·O小记
看过很多的游记,也看过很多的退役记.回忆录,而当自己真正去面对的那一刻,却又不知道从何说起,也不知道能用怎样的形式和语言,才能把这段珍贵的记忆封存起来,留作青春里的一颗璀璨明珠…… 还是随便写写吧…… ...
- linux redhat 安装了jdk检查版本不是自己安装的版本的解决办法
Linux下安装jdk java -version 不是自己所需要的版本 设置环境变量,这是最重要的 在etc/profile文件下添加 export JAVA_HOME=/usr/java/jdk1 ...
- 基于docker部署zabbix
基础环境 cat /etc/redhat-release CentOS Linux release (Core) docker安装 配置yum源 # vim /etc/yum.repos.d/dock ...
- sklearn.GridSearchCV选择超参
from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.model ...
- SQL注入自学[第一学:一个简单的注入环境的编写]
/* 转载请注明出处 ID:珍惜少年时 */ CODE区域: /*注:现在mysql_connect的这种连接方式已经被放弃了,也就是说不用了,老夫也是新手上路故,下载了一个wampserver2.2 ...