当前位置:网站首页>Addition and Subtraction of Scores in LeetCode Medium Questions
Addition and Subtraction of Scores in LeetCode Medium Questions
2022-07-31 03:02:00 【·The sea of stars】
题目
给定一个表示分数加减运算的字符串 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)
解题思路
Using a simple stack was simulated,Due to the requirements of calculation in the title only addition and subtraction,So we default addition and every number with symbols withpython自带的eval进行计算,After every two Numbers for peace. In the stack until the elements in the stack a left.
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
边栏推荐
- Mysql 45讲学习笔记(二十三)MYSQL怎么保证数据不丢
- Modbus on AT32 MCU
- YOLOV5学习笔记(三)——网络模块详解
- AI software development process in medical imaging field
- [Android] Room - Alternative to SQLite
- print task sorting js od huawei
- Why is String immutable?
- Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
- 10. Redis implements likes (Set) and obtains the total number of likes
- 递归查询单表-单表树结构-(自用)
猜你喜欢
分布式与集群是什么 ? 区别是什么?
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
19.支持向量机-优化目标和大间距直观理解
Discourse 自定义头部链接(Custom Header Links)
TCP详解(三)
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
Local area network computer hardware information collection tool
The simulation application of common mode inductance is here, full of dry goods for everyone
编译Hudi
Chapter 9 SVM实践
随机推荐
Modbus on AT32 MCUs
Discussion on Service Commitment of Class Objects under Multithreading
分布式与集群是什么 ? 区别是什么?
Multilingual settings of php website (IP address distinguishes domestic and foreign)
11. Redis implements follow, unfollow, and follow and follower lists
字体压缩神器font-spider的使用
注解用法含义
What is distributed and clustered?What is the difference?
共模电感的仿真应用来了,满满的干货送给大家
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
点云DBSCAN聚类(MATLAB,非内置函数)
Go 项目实战-获取多级分类下的全部商品
冒泡排序、选择排序、直接插入排序、二分法查找
Software accumulation -- Screenshot software ScreenToGif
Compile Hudi
测试中的误报和漏报同样的值得反复修正
下载jar包的好地方
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
10、Redis实现点赞(Set)和获取总点赞数
Discourse 自定义头部链接(Custom Header Links)