当前位置:网站首页>【LeetCode】592. 分数加减运算
【LeetCode】592. 分数加减运算
2022-07-27 12:52:00 【pass night】
题目
给定一个表示分数加减运算的字符串 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 位整数范围内的有效整数。
思路
- 遍历字符串,通过
=-符号将字符串分隔为多个分数运算 - 分数运算的方式为:
- 将分数分隔成分子和分母,并转化为整数
- 通分为两个分母的积
- 进行运算
- 约分:分子和分母除以他们的最大公因数
代码
class Solution:
def fractionAddition(self, expression: str) -> str:
def calc(a: str, b: str, operator: str):
operands = a.split("/")
a1 = int(operands[0])
a2 = int(operands[1])
operands = b.split("/")
b1 = int(operands[0])
b2 = int(operands[1])
b3 = a2*b2
a3 = a1 * b2 + a2*b1 if operator == "+" else a1 * b2 - a2*b1
mod = math.gcd(a3,b3)
a3 //= mod
b3 //= mod
return F"{
a3}/{
b3}"
if expression[0] == "-": expression = "0/1"+expression
if len(expression) <3: return expression
i = 0
while i < len(expression) and expression[i] not in "+-": i += 1
ret = expression[:i]
while i < len(expression):
start = i
i+=1
while i < len(expression) and expression[i] not in "+-": i += 1
ret = calc(ret, expression[start+1:i], expression[start])
return ret
复杂度
- 时间复杂度: O ( n + log C ) O(n+\log C) O(n+logC)
- 空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- v-show
- js回调函数(callback)
- Evconnlistener of libevent_ new_ bind
- Common distributed theories (cap, base) and consistency protocols (gosssip, raft)
- 附加:【URLEncoder.encode(待编码字符串, “编码方式“);】(是什么?;我们向cookie中设置值的时候,为什么要使用这个去编码?)(待完善……)
- Qt优秀开源项目之十三:QScintilla
- [basic knowledge] ~ IC design process and EDA tools used in each stage
- 16-VMware Horizon 2203 虚拟桌面-Win10 自动桌面池完整克隆专用(十六)
- 51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;(只开发了【用户名+密码的,方式】;【@T…】注解控制事务;设置cookie时,是否需要使用URLEncoder去编码;)
- [2023 Fudan Microelectronics written examination questions in advance] ~ questions and reference answers
猜你喜欢

SCI论文写作

How to pass parameters in JNI program

基于frp实现内网穿透——借助公网服务器实现ssh远程连接内网服务器

网络异常流量分析系统设计

Realize the disk partition and file system mount of the newly added hard disk

责任链模式在转转精准估价中的应用

2022年7月24日 暑假第二周训练

纵横靶场-图片的奥秘
![51: Chapter 5: develop admin management services: 4: develop [add admin account, interface]; (only [user name + password, method]; [@t...] annotation controls transactions; when setting cookies, do yo](/img/6f/4f93eca1d923a58b2ef4b1947538be.png)
51: Chapter 5: develop admin management services: 4: develop [add admin account, interface]; (only [user name + password, method]; [@t...] annotation controls transactions; when setting cookies, do yo

eBPF/Ftrace
随机推荐
v-on基础指令
Musk was exposed to be the founder of Google: he broke up his best friend's second marriage and knelt down to beg for forgiveness
接口测试实战教程01:接口测试环境搭建
7.26 simulation summary
Double material first!
Bank case | ZABBIX cross version upgrade guide, isn't 4.2-6.0 popular?
QT excellent open source project 13: qscintilla
[2023 Fudan Microelectronics written examination questions in advance] ~ questions and reference answers
Icon Font
以科技传递温度,vivo亮相数字中国建设峰会
Pat class B 1109 good at C (detailed)
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(九)
W3school navigation bar exercise
Tencent cloud and the China Federation of industry released the research results of industrial AI quality inspection standardization to accelerate the intelligent transformation of manufacturing indus
clear
eBPF/Ftrace
52:第五章:开发admin管理服务:5:开发【分页查询admin账号列表,接口】;(Swagger的@ApiParam(),对方法参数进行注释;PageHelper分页插件;拦截器拦截检查登录状态)
MFC FTP创建多级文件夹、上传文件到FTP指定目录
Two call processors of feign
Method of changing thread state