java.lang.Object
g0501_0600.s0592_fraction_addition_and_subtraction.Solution

public class Solution extends Object
592 - Fraction Addition and Subtraction.<p>Medium</p> <p>Given a string <code>expression</code> representing an expression of fraction addition and subtraction, return the calculation result in string format.</p> <p>The final result should be an <a href="https://en.wikipedia.org/wiki/Irreducible_fraction" target="_top">irreducible fraction</a>. If your final result is an integer, change it to the format of a fraction that has a denominator <code>1</code>. So in this case, <code>2</code> should be converted to <code>2/1</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> expression = &ldquo;-1/2+1/2&rdquo;</p> <p><strong>Output:</strong> &ldquo;0/1&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> expression = &ldquo;-1/2+1/2+1/3&rdquo;</p> <p><strong>Output:</strong> &ldquo;1/3&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> expression = &ldquo;1/3-1/2&rdquo;</p> <p><strong>Output:</strong> &ldquo;-1/6&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li>The input string only contains <code>'0'</code> to <code>'9'</code>, <code>'/'</code>, <code>'+'</code> and <code>'-'</code>. So does the output.</li> <li>Each fraction (input and output) has the format <code>±numerator/denominator</code>. If the first input fraction or the output is positive, then <code>'+'</code> will be omitted.</li> <li>The input only contains valid <strong>irreducible fractions</strong> , where the <strong>numerator</strong> and <strong>denominator</strong> of each fraction will always be in the range <code>[1, 10]</code>. If the denominator is <code>1</code>, it means this fraction is actually an integer in a fraction format defined above.</li> <li>The number of given fractions will be in the range <code>[1, 10]</code>.</li> <li>The numerator and denominator of the <strong>final result</strong> are guaranteed to be valid and in the range of <strong>32-bit</strong> int.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • fractionAddition

      public String fractionAddition(String expression)