-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0640-solve-the-equation.js
55 lines (49 loc) · 1.66 KB
/
0640-solve-the-equation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 640. Solve the Equation
* https://leetcode.com/problems/solve-the-equation/
* Difficulty: Medium
*
* Solve a given equation and return the value of 'x' in the form of a string "x=#value".
* The equation contains only '+', '-' operation, the variable 'x' and its coefficient.
* You should return "No solution" if there is no solution for the equation, or "Infinite
* solutions" if there are infinite solutions for the equation.
*
* If there is exactly one solution for the equation, we ensure that the value of 'x' is
* an integer.
*/
/**
* @param {string} equation
* @return {string}
*/
var solveEquation = function(equation) {
const [left, right] = equation.split('=');
const leftSide = parseSide(left);
const rightSide = parseSide(right);
const totalX = leftSide.xCount - rightSide.xCount;
const totalNum = rightSide.numSum - leftSide.numSum;
if (totalX === 0 && totalNum === 0) return 'Infinite solutions';
if (totalX === 0) return 'No solution';
return `x=${totalNum / totalX}`;
function parseSide(side) {
let xCount = 0;
let numSum = 0;
let currentNum = 0;
let sign = 1;
for (let i = 0; i < side.length; i++) {
const char = side[i];
if (char === 'x') {
const v = i === 0 || side[i-1] === '+' || side[i-1] === '-';
xCount += sign * (currentNum === 0 && (v) ? 1 : currentNum);
currentNum = 0;
} else if (char === '+' || char === '-') {
numSum += sign * currentNum;
sign = char === '+' ? 1 : -1;
currentNum = 0;
} else {
currentNum = currentNum * 10 + parseInt(char);
}
}
numSum += sign * currentNum;
return { xCount, numSum };
}
};