package leetcode;

/**
 * https://leetcode.com/problems/fraction-addition-and-subtraction/
 */
public class Problem592 {
    public String fractionAddition(String expression) {
        // TODO
        return "";
    }

    public static void main(String[] args) {
        Problem592 prob = new Problem592();
        System.out.println(prob.fractionAddition("-1/2+1/2")); // 0/1
        System.out.println(prob.fractionAddition("-1/2+1/2+1/3")); // 1/3
        System.out.println(prob.fractionAddition("1/3-1/2")); // -1/6
        System.out.println(prob.fractionAddition("5/3+1/3")); // 2/1
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -234,129 +234,661 @@
     
-// TODO%0A        return %22%22;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem592 prob = new Problem592
+List%3CInteger%3E numerators = new ArrayList%3C%3E();%0A        List%3CInteger%3E denominators = new ArrayList%3C%3E();%0A        int i = 0;%0A        while (i %3C expression.length()) %7B%0A            char ch = expression.charAt(i);%0A            if (ch == '-' %7C%7C ch == '+') %7B%0A                // get the numerator%0A                i++;%0A                StringBuilder number = new StringBuilder();%0A                while (i %3C expression.length() && expression.charAt(i) %3E= '0' && expression.charAt(i) %3C= '9') %7B%0A                    number.append(expression.charAt(i));%0A                    i++;%0A                %7D%0A                Integer num = Integer.valueOf(%22%22 + ch + number.toString
 ()
+)
 ;%0A  
@@ -897,282 +897,1539 @@
     
+   
-System.out.println(prob.fractionAddition(%22-1/2+1/2%22)); // 0/1%0A        System.out.println(prob.fractionAddition(%22-1/2+1/2+1/3%22)); // 1/3%0A        System.out.println(prob.fractionAddition(%221/3-1/2%22)); // -1/6%0A        System.out.println(prob.fractionAddition(%225/3+1/3%22)); // 2/1
+     numerators.add(num);%0A            %7D else if (ch == '/') %7B%0A                // get the denominator%0A                i++;%0A                StringBuilder number = new StringBuilder();%0A                while (i %3C expression.length() && expression.charAt(i) %3E= '0' && expression.charAt(i) %3C= '9') %7B%0A                    number.append(expression.charAt(i));%0A                    i++;%0A                %7D%0A                Integer denom = Integer.valueOf(%22%22 + number.toString());%0A                denominators.add(denom);%0A            %7D else %7B%0A                // if the expression doesn't start with a sign%0A                StringBuilder number = new StringBuilder();%0A                while (i %3C expression.length() && expression.charAt(i) %3E= '0' && expression.charAt(i) %3C= '9') %7B%0A                    number.append(expression.charAt(i));%0A                    i++;%0A                %7D%0A                Integer num = Integer.valueOf(%22%22 + number.toString());%0A                numerators.add(num);%0A            %7D%0A        %7D%0A        int denom = denominators.get(0);%0A        for (int j = 1; j %3C denominators.size(); j++) %7B%0A            denom *= denominators.get(j);%0A        %7D%0A        int num = 0;%0A        for (int j = 0; j %3C numerators.size(); j++) %7B%0A            num += numerators.get(j) * (denom / denominators.get(j));%0A        %7D%0A        int gcd = Math.abs(gcd(num, denom));%0A        return (num / gcd) + %22/%22 + (denom / gcd);%0A    %7D%0A%0A    private static int gcd(int a, int b) %7B%0A        if (b == 0) %7B%0A            return a;%0A        %7D%0A        return gcd(b, a %25 b);
 %0A   

Solve problem 592