当前位置:网站首页>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

边栏推荐
- CloudCompare&PCL 计算两个点云之间的重叠度
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- Multilingual settings of php website (IP address distinguishes domestic and foreign)
- 递归查询单表-单表树结构-(自用)
- CentOS7下mysql5.7.37的安装【完美方案】
- The use of font compression artifact font-spider
- Installation, start and stop of redis7 under Linux
- php 网站的多语言设置(IP地址区分国内国外)
- 品牌广告投放平台的中台化应用与实践
- 【C语言】三子棋(经典解法+一览图)
猜你喜欢

跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!

Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care

5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
![Installation of mysql5.7.37 under CentOS7 [perfect solution]](/img/ef/a89d8bfd09377dc30034bad99dfd07.png)
Installation of mysql5.7.37 under CentOS7 [perfect solution]

软件积累 -- 截图软件ScreenToGif

SQL injection Less54 (limited number of SQL injection + union injection)

Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!

Compile Hudi

【Android】Room —— SQLite的替代品

接口测试关键技术
随机推荐
选好冒烟测试用例,为进入QA的制品包把好第一道关
CentOS7下mysql5.7.37的安装【完美方案】
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
Maximum area of solar panel od js
8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)
Discourse 自定义头部链接(Custom Header Links)
return in try-catch
6、显示评论和回复
学习DAVID数据库(1)
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
Local area network computer hardware information collection tool
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
Mysql 45讲学习笔记(二十四)MYSQL主从一致
多线程下类对象的服务承诺探讨
The use of font compression artifact font-spider
String为什么不可变?
Installation, start and stop of redis7 under Linux
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
开题报告之论文框架
TCP详解(二)