当前位置:网站首页>LeetCode中等题之分数加减运算
LeetCode中等题之分数加减运算
2022-07-31 02:56:00 【·星辰大海】
题目
给定一个表示分数加减运算的字符串 expression ,你需要返回一个字符串形式的计算结果。
这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分母为 1。所以在上述例子中, 2 应该被转换为 2/1。
示例 1:
输入: expression = “-1/2+1/2”
输出: “0/1”
示例 2:
输入: expression = “-1/2+1/2+1/3”
输出: “1/3”
示例 3:
输入: expression = “1/3-1/2”
输出: “-1/6”
提示:
输入和输出字符串只包含 ‘0’ 到 ‘9’ 的数字,以及 ‘/’, ‘+’ 和 ‘-’。
输入和输出分数格式均为 ±分子/分母。如果输入的第一个分数或者输出的分数是正数,则 ‘+’ 会被省略掉。
输入只包含合法的最简分数,每个分数的分子与分母的范围是 [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。
输入的分数个数范围是 [1,10]。
最终结果的分子与分母保证是 32 位整数范围内的有效整数。
来源:力扣(LeetCode)
解题思路
利用一个简单的栈进行模拟,由于题目中要求的计算只有加减法,所以我们默认加法然后每个数字自带符号用python自带的eval进行计算,每次两个数字进行求和之后再放入栈中直到栈中的元素剩下一个为止。
class Solution:
def fractionAddition(self, expression: str) -> str:
if expression[0].isdigit():
expression='+'+expression
expression+='+'
temp=expression[0]
stack=[]
for i in expression[1:]:
if i=='-' or i=='+':
stack.append(temp)
temp=i
continue
temp+=i
while len(stack)!=1:
left=stack.pop()
right=stack.pop()
lnumerator,ldenominator=left[1:].split('/')
rnumerator,rdenominator=right[1:].split('/')
denominator=eval(ldenominator+'*'+rdenominator)
numerator=eval(rdenominator+'*'+left[0]+lnumerator+'+'+ldenominator+'*'+right[0]+rnumerator)
divisor=math.gcd(denominator,numerator)
denominator//=divisor
numerator//=divisor
if numerator>=0:
stack.append('+'+str(numerator)+'/'+str(denominator))
else:
stack.append(str(numerator)+'/'+str(denominator))
ans=stack.pop()
return ans[1:] if ans[0]=='+' else ans
边栏推荐
- The application of AI in the whole process of medical imaging equipment
- StringJoiner详解
- SQL注入 Less54(限制次数的SQL注入+union注入)
- 关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
- Chapter 9 SVM实践
- golang GUI for nuxui — HelloWorld
- 加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
- StringJoiner in detail
- 8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
- [Android] Room - Alternative to SQLite
猜你喜欢
4. Sensitive word filtering (prefix tree)
Office automation case: how to automatically generate period data?
The simulation application of common mode inductance is here, full of dry goods for everyone
【C语言】进制转换一般方法
The whole process scheduling, MySQL and Sqoop
共模电感的仿真应用来了,满满的干货送给大家
学习DAVID数据库(1)
12 磁盘相关命令
AI在医疗影像设备全流程应用
STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
随机推荐
How to do a startup CTO?
完整复制虚拟机原理(云计算)
Go 项目实战-获取多级分类下的全部商品
Mathematics to solve the problem - circular linked list
【银行系列第一期】中国人民银行
Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
多线程下类对象的服务承诺探讨
4. Sensitive word filtering (prefix tree)
What is a distributed lock?Three ways of implementing distributed lock
软件积累 -- 截图软件ScreenToGif
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
The application of AI in the whole process of medical imaging equipment
SQL 面试用题(重点)
The Sad History of Image Processing Technology
CentOS7下mysql5.7.37的卸载【完美方案】
Discussion on Service Commitment of Class Objects under Multithreading