zoukankan      html  css  js  c++  java
  • [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:

    1. The input string only contains '0' to '9''/''+' and '-'. So does the output.
    2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
    3. 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.
    4. The number of given fractions will be in the range [1,10].
    5. 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"
    

    说明:

    1. 输入和输出字符串只包含 '0' 到 '9' 的数字,以及 '/''+' 和 '-'。 
    2. 输入和输出分数格式均为 ±分子/分母。如果输入的第一个分数或者输出的分数是正数,则 '+' 会被省略掉。
    3. 输入只包含合法的最简分数,每个分数的分子与分母的范围是  [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。
    4. 输入的分数个数范围是 [1,10]。
    5. 最终结果的分子与分母保证是 32 位整数范围内的有效整数。

    Runtime: 8 ms
    Memory Usage: 19.4 MB
     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 }
  • 相关阅读:
    【接口平台】上报接口处理时间
    【性能测试】吞吐量上不去的问题
    vue层级选择器多选
    打家劫舍 II
    打家劫舍
    房屋染色 II
    距离顺序排列矩阵单元格
    根据身高重建队列
    将 x 减到 0 的最小操作数
    确定两个字符串是否接近
  • 原文地址:https://www.cnblogs.com/strengthen/p/10450070.html
Copyright © 2011-2022 走看看