Solve the Equation
Last updated
Last updated
Input:
"2x=x"
Output:
"x=0"Input:
"2x+3x-6x=x+2"
Output:
"x=-1"Input:
"x=x+2"
Output:
"No solution"按特定匹配条件split
String[] tokens = s.split("(?=[+-])");class Solution {
/*
x +5 -2x = 6 -3x;
leftPart : tokens= { x, +5, -2x}; coefficient for x = 1-2 =-1; constant = 5;
rightPart: tokens= {6, -3x}; coefficient for x = -3; constant = 6;
final result = (6-5)/ (-1 - (-3))
*/
public String solveEquation(String equation) {
String[] parts = equation.split("=");
int[] leftPart = evaluate(parts[0]);
int[] rightPart = evaluate(parts[1]);
if (leftPart[0] == rightPart[0] &&
leftPart[1] == rightPart[1]) {
return "Infinite solutions";
} else if (leftPart[0] == rightPart[0]){
return "No solution";
}
return "x="+ (rightPart[1] - leftPart[1]) / (leftPart[0] - rightPart[0]);
}
private int[] evaluate(String s) {
String[] tokens = s.split("(?=[+-])");
// ()for match group; ?= for match and include in res; [+-] means + or -;
int[] res = new int[2];
for(String token : tokens) {
if (token.equals("+x") || token.equals("x")) {
res[0]++; // x means 1x
} else if (token.equals("-x")) {
res[0]--;// -x means -1x
} else if (token.contains("x")) { // +kx or -kx to get k
res[0] += Integer.parseInt(token.substring(0, token.length() - 1));
} else {
res[1] += Integer.parseInt(token);
}
}
return res;
}
}