-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathSolveTheEquation640.java
138 lines (131 loc) · 3.9 KB
/
SolveTheEquation640.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Solve a given equation and return the value of x in the form of string
* "x=#value". The equation contains only '+', '-' operation, the variable x
* and its coefficient.
*
* If there is no solution for the equation, return "No solution".
*
* If there are infinite solutions for the equation, return "Infinite solutions".
*
* If there is exactly one solution for the equation, we ensure that the value
* of x is an integer.
*
* Example 1:
* Input: "x+5-3+x=6+x-2"
* Output: "x=2"
*
* Example 2:
* Input: "x=x"
* Output: "Infinite solutions"
*
* Example 3:
* Input: "2x=x"
* Output: "x=0"
*
* Example 4:
* Input: "2x+3x-6x=x+2"
* Output: "x=-1"
*
* Example 5:
* Input: "x=x+2"
* Output: "No solution"
*/
public class SolveTheEquation640 {
public String solveEquation(String equation) {
char[] chars = equation.toCharArray();
int N = chars.length;
int a = 0;
int b = 0;
int curr = 0;
int sign = 1;
int i = 0;
while (chars[i] != '=') {
if (Character.isDigit(chars[i])) {
curr = curr * 10 + (int)(chars[i] - '0');
if (chars[i+1] == '=') {
i += 2;
break;
}
} else if (chars[i] == '+') {
b += sign * curr;
curr = 0;
sign = 1;
} else if (chars[i] == '-') {
b += sign * curr;
curr = 0;
sign = -1;
} else {
if (i == 0 || chars[i-1] != '0') {
a += sign * (curr == 0 ? 1 : curr);
}
curr = 0;
i++;
if (chars[i] != '=') {
sign = chars[i] == '+' ? 1 : -1;
} else {
sign = 0;
i++;
break;
}
}
i++;
}
if (sign != 0) {
b += sign * curr;
}
curr = 0;
sign = 1;
while (i < N) {
if (Character.isDigit(chars[i])) {
curr = curr * 10 + (int)(chars[i] - '0');
} else if (chars[i] == '+') {
b -= sign * curr;
curr = 0;
sign = 1;
} else if (chars[i] == '-') {
b -= sign * curr;
curr = 0;
sign = -1;
} else {
if (i == 0 || chars[i-1] != '0') {
a -= sign * (curr == 0 ? 1 : curr);
}
curr = 0;
i++;
if (i != N) {
sign = chars[i] == '+' ? 1 : -1;
}
}
i++;
}
b -= sign * curr;
if (a == 0) {
return b == 0 ? "Infinite solutions" : "No solution";
} else {
return "x=" + (- b / a);
}
}
/**
* https://leetcode.com/problems/solve-the-equation/discuss/105311/Concise-Java-Solution
*/
public String solveEquation2(String equation) {
int[] res = evaluateExpression(equation.split("=")[0]);
int[] res2 = evaluateExpression(equation.split("=")[1]);
res[0] -= res2[0];
res[1] = res2[1] - res[1];
if (res[0] == 0 && res[1] == 0) return "Infinite solutions";
if (res[0] == 0) return "No solution";
return "x=" + res[1]/res[0];
}
public int[] evaluateExpression(String exp) {
String[] tokens = exp.split("(?=[-+])");
int[] res = new int[2];
for (String token : tokens) {
if (token.equals("+x") || token.equals("x")) res[0] += 1;
else if (token.equals("-x")) res[0] -= 1;
else if (token.contains("x")) res[0] += Integer.parseInt(token.substring(0, token.indexOf("x")));
else res[1] += Integer.parseInt(token);
}
return res;
}
}