[Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10450070.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2
, you need to change it to the format of fraction that has denominator 1
. So in this case, 2
should be converted to 2/1
.
Example 1:
Input:"-1/2+1/2" Output: "0/1"
Example 2:
Input:"-1/2+1/2+1/3" Output: "1/3"
Example 3:
Input:"1/3-1/2" Output: "-1/6"
Example 4:
Input:"5/3+1/3" Output: "2/1"
Note:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has format
±numerator/denominator
. If the first input fraction or the output is positive, then'+'
will be omitted. - The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
- The number of given fractions will be in the range [1,10].
- The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果。 这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2
,你需要将它转换成分数形式,其分母为 1
。所以在上述例子中, 2
应该被转换为 2/1
。
示例 1:
输入:"-1/2+1/2" 输出: "0/1"
示例 2:
输入:"-1/2+1/2+1/3" 输出: "1/3"
示例 3:
输入:"1/3-1/2" 输出: "-1/6"
示例 4:
输入:"5/3+1/3" 输出: "2/1"
说明:
- 输入和输出字符串只包含
'0'
到'9'
的数字,以及'/'
,'+'
和'-'
。 - 输入和输出分数格式均为
±分子/分母
。如果输入的第一个分数或者输出的分数是正数,则'+'
会被省略掉。 - 输入只包含合法的最简分数,每个分数的分子与分母的范围是 [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。
- 输入的分数个数范围是 [1,10]。
- 最终结果的分子与分母保证是 32 位整数范围内的有效整数。
1 class Solution { 2 func fractionAddition(_ expression: String) -> String { 3 var n = 0 4 var d = 1 5 var s = Array(expression) 6 if s[0] != "-" { 7 s.insert("+", at: 0) 8 } 9 var p = 0 10 while p < s.count { 11 var p1 = p + 1 12 while s[p1] != "/" { 13 p1 += 1 14 } 15 var p2 = p1 + 1 16 while p2 < s.count && s[p2] != "+" && s[p2] != "-" { 17 p2 += 1 18 } 19 20 let nn = Int(String(s[p+1..<p1]))! 21 let dd = Int(String(s[p1+1..<p2]))! 22 let g = gcd(d, dd) 23 24 n = n * dd / g + (s[p] == "-" ? -1 : 1) * nn * d / g 25 d *= dd / g 26 p = p2 27 } 28 29 let g = gcd(abs(n), d) 30 return String(n / g) + "/" + String(d / g) 31 } 32 33 func gcd(_ a: Int, _ b: Int) -> Int { 34 return (b == 0) ? a: gcd(b, a % b) 35 } 36 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了