Skip to content

Latest commit

 

History

History
11 lines (6 loc) · 1.16 KB

File metadata and controls

11 lines (6 loc) · 1.16 KB

592. Fraction Addition and Subtraction

Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.

The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.

let's think about a brute force idea first. we need to calcualte a minimal common multiple of all the denominators. Then we can add all the numerators and divide the result by the common denominator. reduce them by overall gcd. this is a very naive idea. we can do better. But let me implement it first.

Yep, accept, careful about the denominaotr and numerator processing, because it's will be bigger than 10, and you should also be careful to find the position of next '+' or '-' and compare if the finding result is -1 or not.

I see the std using the gcd and lca item by item. Instead of using the gcd and lca of all the items, I think it's better to use the gcd and lca of the first item and the next item. Because the gcd and lca of the first item and the next item is smaller and easy to calculate.