|
| 1 | +package com.stevesun.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * 592. Fraction Addition and Subtraction |
| 8 | + * |
| 9 | + * Given a string representing an expression of fraction addition and subtraction, |
| 10 | + * you need to return the calculation result in string format. |
| 11 | + * The final result should be irreducible fraction. |
| 12 | + * If your final result is an integer, |
| 13 | + * say 2, you need to change it to the format of fraction that has denominator 1. |
| 14 | + * So in this case, 2 should be converted to 2/1. |
| 15 | +
|
| 16 | + Example 1: |
| 17 | + Input:"-1/2+1/2" |
| 18 | + Output: "0/1" |
| 19 | +
|
| 20 | + Example 2: |
| 21 | + Input:"-1/2+1/2+1/3" |
| 22 | + Output: "1/3" |
| 23 | +
|
| 24 | + Example 3: |
| 25 | + Input:"1/3-1/2" |
| 26 | + Output: "-1/6" |
| 27 | +
|
| 28 | + Example 4: |
| 29 | + Input:"5/3+1/3" |
| 30 | + Output: "2/1" |
| 31 | +
|
| 32 | + Note: |
| 33 | + The input string only contains '0' to '9', '/', '+' and '-'. So does the output. |
| 34 | + Each fraction (input and output) has format ±numerator/denominator. |
| 35 | + If the first input fraction or the output is positive, then '+' will be omitted. |
| 36 | + The input only contains valid irreducible fractions, |
| 37 | + where the numerator and denominator of each fraction will always be in the range [1,10]. |
| 38 | + If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above. |
| 39 | + The number of given fractions will be in the range [1,10]. |
| 40 | + The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int. |
| 41 | + */ |
| 42 | +public class _592 { |
| 43 | + |
| 44 | + /**Credit: https://discuss.leetcode.com/topic/89993/java-solution-fraction-addition-and-gcd*/ |
| 45 | + public String fractionAddition(String expression) { |
| 46 | + List<String> nums = new ArrayList<>(); |
| 47 | + int i = 0; |
| 48 | + int j = 0; |
| 49 | + while (j <= expression.length()) { |
| 50 | + if (j == expression.length() || j != i && (expression.charAt(j) == '-' || expression.charAt(j) == '+')) { |
| 51 | + if (expression.charAt(i) == '+') { |
| 52 | + nums.add(expression.substring(i+1, j)); |
| 53 | + } else { |
| 54 | + nums.add(expression.substring(i, j)); |
| 55 | + } |
| 56 | + i = j; |
| 57 | + } |
| 58 | + j++; |
| 59 | + } |
| 60 | + |
| 61 | + String result = "0/1"; |
| 62 | + for (String frac : nums) { |
| 63 | + result = add(result, frac); |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private String add(String result, String frac) { |
| 69 | + String[] frac1 = frac.split("/"); |
| 70 | + String[] frac2 = result.split("/"); |
| 71 | + int n1 = Integer.parseInt(frac1[0]); |
| 72 | + int d1 = Integer.parseInt(frac1[1]); |
| 73 | + int n2 = Integer.parseInt(frac2[0]); |
| 74 | + int d2 = Integer.parseInt(frac2[1]); |
| 75 | + int numerator = n1*d2 + n2*d1; |
| 76 | + int denominator = d1*d2; |
| 77 | + if (numerator == 0) return "0/1"; |
| 78 | + |
| 79 | + boolean negative = numerator*denominator < 0; |
| 80 | + numerator = Math.abs(numerator); |
| 81 | + denominator = Math.abs(denominator); |
| 82 | + int gcd = getGCD(numerator, denominator); |
| 83 | + |
| 84 | + return (negative ? "-" : "") + (numerator/gcd) + "/" + (denominator/gcd); |
| 85 | + } |
| 86 | + |
| 87 | + private int getGCD(int a, int b) { |
| 88 | + if (a == 0 || b == 0) return a + b; |
| 89 | + return getGCD(b, a%b); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments