Skip to content

Commit dfeebfa

Browse files
committed
feat: add solutions to lc problem: No.0640
No.0640.Solve the Equation
1 parent 9f0f485 commit dfeebfa

File tree

5 files changed

+346
-2
lines changed

5 files changed

+346
-2
lines changed

solution/0600-0699/0640.Solve the Equation/README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,145 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:数学**
55+
56+
将方程 $equation$ 按照等号 “=” 切分为左右两个式子,分别算出左右两个式子中 "x" 的系数 $x_i$,以及常数的值 $y_i$。
57+
58+
那么方程转换为等式 $x_1 \times x + y_1 = x_2 \times x + y_2$。
59+
60+
- 当 $x_1 = x_2$:若 $y_1 \neq y_2$,方程无解;若 $y_1=y_2$,方程有无限解。
61+
- 当 $x_1 \neq x_2$:方程有唯一解 $x=\frac{y_2-y_1}{x_1-x_2}$。
62+
63+
相似题目:[592. 分数加减运算](/solution/0500-0599/0592.Fraction%20Addition%20and%20Subtraction/README.md)
64+
5465
<!-- tabs:start -->
5566

5667
### **Python3**
5768

5869
<!-- 这里可写当前语言的特殊实现逻辑 -->
5970

6071
```python
61-
72+
class Solution:
73+
def solveEquation(self, equation: str) -> str:
74+
def f(s):
75+
x = y = 0
76+
if s[0] != '-':
77+
s = '+' + s
78+
i, n = 0, len(s)
79+
while i < n:
80+
sign = 1 if s[i] == '+' else -1
81+
i += 1
82+
j = i
83+
while j < n and s[j] not in '+-':
84+
j += 1
85+
v = s[i: j]
86+
if v[-1] == 'x':
87+
x += sign * (int(v[:-1]) if len(v) > 1 else 1)
88+
else:
89+
y += sign * int(v)
90+
i = j
91+
return x, y
92+
93+
a, b = equation.split('=')
94+
x1, y1 = f(a)
95+
x2, y2 = f(b)
96+
if x1 == x2:
97+
return 'Infinite solutions' if y1 == y2 else 'No solution'
98+
return f'x={(y2 - y1) // (x1 - x2)}'
6299
```
63100

64101
### **Java**
65102

66103
<!-- 这里可写当前语言的特殊实现逻辑 -->
67104

68105
```java
106+
class Solution {
107+
public String solveEquation(String equation) {
108+
String[] es = equation.split("=");
109+
int[] a = f(es[0]), b = f(es[1]);
110+
int x1 = a[0], y1 = a[1];
111+
int x2 = b[0], y2 = b[1];
112+
if (x1 == x2) {
113+
return y1 == y2 ? "Infinite solutions" : "No solution";
114+
}
115+
return "x=" + (y2 - y1) / (x1 - x2);
116+
}
117+
118+
private int[] f(String s) {
119+
int x = 0, y = 0;
120+
if (s.charAt(0) != '-') {
121+
s = "+" + s;
122+
}
123+
int i = 0, n = s.length();
124+
while (i < n) {
125+
int sign = s.charAt(i) == '+' ? 1 : -1;
126+
++i;
127+
int j = i;
128+
while (j < n && s.charAt(j) != '+' && s.charAt(j) != '-') {
129+
++j;
130+
}
131+
String v = s.substring(i, j);
132+
if (s.charAt(j - 1) == 'x') {
133+
x += sign * (v.length() > 1 ? Integer.parseInt(v.substring(0, v.length() - 1)) : 1);
134+
} else {
135+
y += sign * Integer.parseInt(v);
136+
}
137+
i = j;
138+
}
139+
return new int[]{x, y};
140+
}
141+
}
142+
```
69143

144+
### **Go**
145+
146+
```go
147+
func solveEquation(equation string) string {
148+
f := func(s string) []int {
149+
x, y := 0, 0
150+
if s[0] != '-' {
151+
s = "+" + s
152+
}
153+
i, n := 0, len(s)
154+
for i < n {
155+
sign := 1
156+
if s[i] == '-' {
157+
sign = -1
158+
}
159+
i++
160+
j := i
161+
for j < n && s[j] != '+' && s[j] != '-' {
162+
j++
163+
}
164+
v := s[i:j]
165+
if s[j-1] == 'x' {
166+
a := 1
167+
if len(v) > 1 {
168+
a, _ = strconv.Atoi(v[:len(v)-1])
169+
}
170+
x += sign * a
171+
} else {
172+
a, _ := strconv.Atoi(v)
173+
y += sign * a
174+
}
175+
i = j
176+
}
177+
return []int{x, y}
178+
}
179+
180+
es := strings.Split(equation, "=")
181+
a, b := f(es[0]), f(es[1])
182+
x1, y1 := a[0], a[1]
183+
x2, y2 := b[0], b[1]
184+
if x1 == x2 {
185+
if y1 == y2 {
186+
return "Infinite solutions"
187+
} else {
188+
return "No solution"
189+
}
190+
}
191+
return fmt.Sprintf("x=%d", (y2-y1)/(x1-x2))
192+
}
70193
```
71194

72195
### **...**

solution/0600-0699/0640.Solve the Equation/README_EN.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,125 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def solveEquation(self, equation: str) -> str:
51+
def f(s):
52+
x = y = 0
53+
if s[0] != '-':
54+
s = '+' + s
55+
i, n = 0, len(s)
56+
while i < n:
57+
sign = 1 if s[i] == '+' else -1
58+
i += 1
59+
j = i
60+
while j < n and s[j] not in '+-':
61+
j += 1
62+
v = s[i: j]
63+
if v[-1] == 'x':
64+
x += sign * (int(v[:-1]) if len(v) > 1 else 1)
65+
else:
66+
y += sign * int(v)
67+
i = j
68+
return x, y
69+
70+
a, b = equation.split('=')
71+
x1, y1 = f(a)
72+
x2, y2 = f(b)
73+
if x1 == x2:
74+
return 'Infinite solutions' if y1 == y2 else 'No solution'
75+
return f'x={(y2 - y1) // (x1 - x2)}'
5076
```
5177

5278
### **Java**
5379

5480
```java
81+
class Solution {
82+
public String solveEquation(String equation) {
83+
String[] es = equation.split("=");
84+
int[] a = f(es[0]), b = f(es[1]);
85+
int x1 = a[0], y1 = a[1];
86+
int x2 = b[0], y2 = b[1];
87+
if (x1 == x2) {
88+
return y1 == y2 ? "Infinite solutions" : "No solution";
89+
}
90+
return "x=" + (y2 - y1) / (x1 - x2);
91+
}
92+
93+
private int[] f(String s) {
94+
int x = 0, y = 0;
95+
if (s.charAt(0) != '-') {
96+
s = "+" + s;
97+
}
98+
int i = 0, n = s.length();
99+
while (i < n) {
100+
int sign = s.charAt(i) == '+' ? 1 : -1;
101+
++i;
102+
int j = i;
103+
while (j < n && s.charAt(j) != '+' && s.charAt(j) != '-') {
104+
++j;
105+
}
106+
String v = s.substring(i, j);
107+
if (s.charAt(j - 1) == 'x') {
108+
x += sign * (v.length() > 1 ? Integer.parseInt(v.substring(0, v.length() - 1)) : 1);
109+
} else {
110+
y += sign * Integer.parseInt(v);
111+
}
112+
i = j;
113+
}
114+
return new int[]{x, y};
115+
}
116+
}
117+
```
55118

119+
### **Go**
120+
121+
```go
122+
func solveEquation(equation string) string {
123+
f := func(s string) []int {
124+
x, y := 0, 0
125+
if s[0] != '-' {
126+
s = "+" + s
127+
}
128+
i, n := 0, len(s)
129+
for i < n {
130+
sign := 1
131+
if s[i] == '-' {
132+
sign = -1
133+
}
134+
i++
135+
j := i
136+
for j < n && s[j] != '+' && s[j] != '-' {
137+
j++
138+
}
139+
v := s[i:j]
140+
if s[j-1] == 'x' {
141+
a := 1
142+
if len(v) > 1 {
143+
a, _ = strconv.Atoi(v[:len(v)-1])
144+
}
145+
x += sign * a
146+
} else {
147+
a, _ := strconv.Atoi(v)
148+
y += sign * a
149+
}
150+
i = j
151+
}
152+
return []int{x, y}
153+
}
154+
155+
es := strings.Split(equation, "=")
156+
a, b := f(es[0]), f(es[1])
157+
x1, y1 := a[0], a[1]
158+
x2, y2 := b[0], b[1]
159+
if x1 == x2 {
160+
if y1 == y2 {
161+
return "Infinite solutions"
162+
} else {
163+
return "No solution"
164+
}
165+
}
166+
return fmt.Sprintf("x=%d", (y2-y1)/(x1-x2))
167+
}
56168
```
57169

58170
### **...**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
func solveEquation(equation string) string {
2+
f := func(s string) []int {
3+
x, y := 0, 0
4+
if s[0] != '-' {
5+
s = "+" + s
6+
}
7+
i, n := 0, len(s)
8+
for i < n {
9+
sign := 1
10+
if s[i] == '-' {
11+
sign = -1
12+
}
13+
i++
14+
j := i
15+
for j < n && s[j] != '+' && s[j] != '-' {
16+
j++
17+
}
18+
v := s[i:j]
19+
if s[j-1] == 'x' {
20+
a := 1
21+
if len(v) > 1 {
22+
a, _ = strconv.Atoi(v[:len(v)-1])
23+
}
24+
x += sign * a
25+
} else {
26+
a, _ := strconv.Atoi(v)
27+
y += sign * a
28+
}
29+
i = j
30+
}
31+
return []int{x, y}
32+
}
33+
34+
es := strings.Split(equation, "=")
35+
a, b := f(es[0]), f(es[1])
36+
x1, y1 := a[0], a[1]
37+
x2, y2 := b[0], b[1]
38+
if x1 == x2 {
39+
if y1 == y2 {
40+
return "Infinite solutions"
41+
} else {
42+
return "No solution"
43+
}
44+
}
45+
return fmt.Sprintf("x=%d", (y2-y1)/(x1-x2))
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public String solveEquation(String equation) {
3+
String[] es = equation.split("=");
4+
int[] a = f(es[0]), b = f(es[1]);
5+
int x1 = a[0], y1 = a[1];
6+
int x2 = b[0], y2 = b[1];
7+
if (x1 == x2) {
8+
return y1 == y2 ? "Infinite solutions" : "No solution";
9+
}
10+
return "x=" + (y2 - y1) / (x1 - x2);
11+
}
12+
13+
private int[] f(String s) {
14+
int x = 0, y = 0;
15+
if (s.charAt(0) != '-') {
16+
s = "+" + s;
17+
}
18+
int i = 0, n = s.length();
19+
while (i < n) {
20+
int sign = s.charAt(i) == '+' ? 1 : -1;
21+
++i;
22+
int j = i;
23+
while (j < n && s.charAt(j) != '+' && s.charAt(j) != '-') {
24+
++j;
25+
}
26+
String v = s.substring(i, j);
27+
if (s.charAt(j - 1) == 'x') {
28+
x += sign * (v.length() > 1 ? Integer.parseInt(v.substring(0, v.length() - 1)) : 1);
29+
} else {
30+
y += sign * Integer.parseInt(v);
31+
}
32+
i = j;
33+
}
34+
return new int[]{x, y};
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def solveEquation(self, equation: str) -> str:
3+
def f(s):
4+
x = y = 0
5+
if s[0] != '-':
6+
s = '+' + s
7+
i, n = 0, len(s)
8+
while i < n:
9+
sign = 1 if s[i] == '+' else -1
10+
i += 1
11+
j = i
12+
while j < n and s[j] not in '+-':
13+
j += 1
14+
v = s[i: j]
15+
if v[-1] == 'x':
16+
x += sign * (int(v[:-1]) if len(v) > 1 else 1)
17+
else:
18+
y += sign * int(v)
19+
i = j
20+
return x, y
21+
22+
a, b = equation.split('=')
23+
x1, y1 = f(a)
24+
x2, y2 = f(b)
25+
if x1 == x2:
26+
return 'Infinite solutions' if y1 == y2 else 'No solution'
27+
return f'x={(y2 - y1) // (x1 - x2)}'

0 commit comments

Comments
 (0)