Simple java ideas though a bit longer


  • 0
    S

    Hey guys, my code is a bit longer but easy to understand. If you have any suggestions, please let me know. Thank you!

    public class Solution {
        public String fractionAddition(String expression) {
            if (expression == null || expression.length() == 0) return null;
    
            if (expression.charAt(0) != '-') expression = '+' + expression;
            String clone = expression;
            String[] strings = clone.split("\\+|-"); //use an array to save all the fractions
            int n = strings.length;
    
            boolean[] positive = new boolean[n-1]; //use another array to save all the signs
            int index = 0;
            for (int i = 0; i < expression.length(); i++) {
                if (expression.charAt(i) == '+') {
                    positive[index] = true;
                    index++;
                }
                if (expression.charAt(i) == '-') {
                    positive[index] = false;
                    index++;
                }
            }
    
            Fraction pre = new Fraction(0, 1, true); //start with a fraction 0
    
            for (int i = 1; i < n; i++) { //adding process
                String[] fracSplit = strings[i].split("/");
                int nume = Integer.parseInt(fracSplit[0]);
                int deno = Integer.parseInt(fracSplit[1]);
                pre = addition(pre, new Fraction(nume, deno, positive[i-1]));
            }
    
            String res = "";
            if (!pre.positive) res = res + '-';
            res = res + pre.numerator + "/" + pre.denominator;
            return res;
        }
    
        private Fraction addition(Fraction a, Fraction b) {  //the addition process
            if (a.numerator == 0) return b;
            if (b.numerator == 0) return a;
    
            int aNume = a.numerator, bNume = b.numerator;
            if (!a.positive) aNume = -aNume;
            if (!b.positive) bNume = -bNume;
    
            int minCM = a.denominator;
            if (a.denominator != b.denominator) {
                minCM = minCommonMultiple(a.denominator, b.denominator);
                aNume = minCM / a.denominator * aNume;
                bNume = minCM / b.denominator * bNume;
            }
    
            int nume = aNume + bNume;
            boolean positive = true;
            if (nume < 0) positive = false;
            if (nume == 0) return new Fraction(0, 1, true);
            int maxCD = maxCommonDivisor(Math.abs(nume), minCM);
            if (maxCD != 1) {
                nume = Math.abs(nume) / maxCD;
                minCM = minCM / maxCD;
            }
    
            return new Fraction(Math.abs(nume), minCM, positive);
        }
    
        private int maxCommonDivisor(int x, int y) {
            if (x < y) {
                int temp = x;
                x = y;
                y = temp;
            }
            while (x % y != 0) {
                int temp = x % y;
                x = y;
                y = temp;
            }
            return y;
        }
    
        private int minCommonMultiple(int x, int y) {
            return x * y / maxCommonDivisor(x, y);
        }
    }
    
    class Fraction {
        int numerator;
        int denominator;
        boolean positive;
        Fraction(int numerator, int denominator, boolean positive) {
            this.numerator = numerator;
            this.denominator = denominator;
            this.positive = positive;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.