LeetCode: Fraction Addition and Subtraction Posted on February 22, 2018July 26, 2020 by braindenny Fraction Addition and Subtraction Similar Problems: Solve the Equation CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #math, #expression, #gcd 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 positive sign 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. Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. // https://code.dennyzhang.com/fraction-addition-and-subtraction import "strconv" // https://code.dennyzhang.com/fraction-addition-and-subtraction // Basic Ideas: stack + gcd // // Convert string to list: [sign, num1, '/', num2] // // Use GCD to get the result of two entries // // Complexity: Time O(n), Space O(n) type MyNode struct { numerator int denominator int } func gcd(x, y int) int { if x < 0 { return gcd(-x, y) } if y<0 { return gcd(x, -y) } if x<y { return gcd(y, x) } if y == 0 { return y } for y!= 0 { y, x = x%y, y } return x } func fractionAddition(expression string) string { // Assume string won't be empty and is valid if expression[0] >= '1' && expression[0] <= '9' { expression = string('+')+expression } l := []MyNode{} i:=0 for i<len(expression) { isPositive := true if expression[i] == '-' { isPositive = false } i++ // find numerator: expression[i:j+1] j:=i for j+1<len(expression) && expression[j+1] != '/' { j++ } numerator, _ := strconv.Atoi(string(expression[i:j+1])) if !isPositive { numerator = -numerator } i = j+2 j = i for j+1<len(expression) && expression[j+1] != '-' && expression[j+1] != '+' { j++ } denominator, _ := strconv.Atoi(string(expression[i:j+1])) i = j+1 node := MyNode{numerator:numerator, denominator:denominator} l = append(l, node) } node := MyNode{numerator:0, denominator:1} for _, node2 := range l { n := gcd(node.denominator, node2.denominator) denominator := (node.denominator/n)*node2.denominator numerator := node.numerator*(denominator/node.denominator) numerator += node2.numerator*(denominator/node2.denominator) p := gcd(numerator, denominator) if p != 0 { node = MyNode{numerator:numerator/p, denominator:denominator/p} } else { node = MyNode{numerator:0, denominator:1} } } return fmt.Sprintf("%d/%d", node.numerator, node.denominator) } Post Views: 8