leetcode-求解方程

https://leetcode-cn.com/problems/solve-the-equation/description/ide

/**
 * Created by feichen on 2018/6/17.
 * <p>
 * <p>
 * 求解一個給定的方程,將x以字符串"x=#value"的形式返回。該方程僅包含'+',' - '操做,變量 x 和其對應係數。
 * <p>
 * 若是方程沒有解,請返回「No solution」。
 * <p>
 * 若是方程有無限解,則返回「Infinite solutions」。
 * <p>
 * 若是方程中只有一個解,要保證返回值 x 是一個整數。
 * <p>
 * 示例 1:
 * <p>
 * 輸入: "x+5-3+x=6+x-2"
 * 輸出: "x=2"
 * 示例 2:
 * <p>
 * 輸入: "x=x"
 * 輸出: "Infinite solutions"
 * 示例 3:
 * <p>
 * 輸入: "2x=x"
 * 輸出: "x=0"
 * 示例 4:
 * <p>
 * 輸入: "2x+3x-6x=x+2"
 * 輸出: "x=-1"
 * 示例 5:
 * <p>
 * 輸入: "x=x+2"
 * 輸出: "No solution"
 */


public class SolveEquation {

    /**
     * 方程沒有解
     */
    private static final String NO_SOLUTIONS = "No solution";

    /**
     * 方程有無限解
     */
    private static final String INFINITE_SOLUTIONS = "Infinite solutions";


    class EquationSide {
        /**
         * 有幾個x
         */
        private int countX;

        /**
         * 常數
         */
        private int constantNo;

        public int getCountX() {
            return countX;
        }

        public void setCountX(int countX) {
            this.countX = countX;
        }

        public int getConstantNo() {
            return constantNo;
        }

        public void setConstantNo(int constantNo) {
            this.constantNo = constantNo;
        }

        @Override
        public String toString() {
            return "EquationSide{" +
                    "countX=" + countX +
                    ", constantNo=" + constantNo +
                    '}';
        }
    }


    /**
     * 轉換
     *
     * @param equation
     * @return
     */
    private EquationSide convert(String equation) {
        String[] strings = equation.split("=");
        EquationSide finalEquation = new EquationSide();
        if (strings.length != 2) {
            return finalEquation;
        }
        String left = strings[0];
        String right = strings[1];
        EquationSide leftEquation = convertString(left);
        EquationSide rightEquation = convertString(right);

        finalEquation.setCountX(leftEquation.getCountX() - rightEquation.getCountX());
        finalEquation.setConstantNo(leftEquation.getConstantNo() - rightEquation.getConstantNo());
        return finalEquation;
    }

    /**
     * @param s
     * @return
     */
    private EquationSide convertString(String s) {
        EquationSide equationSide = new EquationSide();
        if (s.length() == 1) {
            if (s.equals("x")) {
                equationSide.setCountX(1);
            } else {
                equationSide.setConstantNo(Integer.valueOf(s));
            }
            return equationSide;
        }
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; ) {
            if (i == chars.length - 1) {
                break;
            }
            for (int j = i + 1; j < chars.length; j++) {
                if (j == chars.length - 1) {
                    if (chars[j] == 'x') {
                        if (j >= 1 && chars[j - 1] != '+' && chars[j - 1] != '-') {
                            equationSide.countX += Integer.valueOf(s.substring(i, j));
                        } else if (j >= 1 && chars[j - 1] == '+') {
                            equationSide.countX++;
                        } else if (j >= 1 && chars[j - 1] == '-') {
                            equationSide.countX--;
                        }
                    } else {
                        equationSide.constantNo += Integer.valueOf(s.substring(i));
                    }
                    i = j;
                    break;
                }
                if (chars[j] == '+' || chars[j] == '-') {
                    if (chars[j - 1] == 'x') {
                        if (j >= 2 && chars[j - 2] != '+' && chars[j - 2] != '-') {
                            equationSide.countX += Integer.valueOf(s.substring(i, j - 1));
                        } else if (j >= 2 && chars[j - 2] == '-') {
                            equationSide.countX--;
                        } else {
                            equationSide.countX++;
                        }
                    } else {
                        equationSide.constantNo += Integer.valueOf(s.substring(i, j));
                    }
                    i = j;
                    break;
                }
            }
        }
        return equationSide;
    }

    public String solveEquation(String equation) {
        EquationSide finalResult = convert(equation);
        if (finalResult.getCountX() == 0 && finalResult.getConstantNo() == 0) {
            return INFINITE_SOLUTIONS;
        } else if (finalResult.getCountX() == 0 && finalResult.getConstantNo() != 0) {
            return NO_SOLUTIONS;
        } else if (finalResult.getCountX() != 0 && finalResult.getConstantNo() == 0) {
            return "x=0";
        } else {
            return "x=" + -(finalResult.getConstantNo() / finalResult.getCountX());
        }
    }


    public static void main(String[] args) {
        SolveEquation solveEquation = new SolveEquation();
        System.out.println(solveEquation.solveEquation("x=x+2"));

    }
}
相關文章
相關標籤/搜索