[Swift]LeetCode592. 分數加減運算 | Fraction Addition and Subtraction

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wvvqrlxu-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git

Example 1:github

Input:"-1/2+1/2"
Output: "0/1" 

Example 2:express

Input:"-1/2+1/2+1/3"
Output: "1/3" 

Example 3:微信

Input:"1/3-1/2"
Output: "-1/6" 

Example 4:this

Input:"5/3+1/3"
Output: "2/1" 

Note:spa

  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/1code

示例 1:orm

輸入:"-1/2+1/2"
輸出: "0/1"

 示例 2:htm

輸入:"-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 }
相關文章
相關標籤/搜索