Skip to content

Commit 7efdc35

Browse files
committed
feat: update solutions to lc problem: No.0990
No.0990.Satisfiability of Equality Equations
1 parent 0fd5389 commit 7efdc35

File tree

6 files changed

+74
-111
lines changed

6 files changed

+74
-111
lines changed

solution/0900-0999/0990.Satisfiability of Equality Equations/README.md

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66-
并查集。
66+
并查集。对于本题,先遍历所有的等式,构造并查集。接着遍历所有不等式,如果不等式的两个变量处于同一个集合,说明发生矛盾,返回 false。否则遍历结束返回 true。
67+
68+
以下是并查集的几个常用模板。
6769

6870
模板 1——朴素并查集:
6971

@@ -122,8 +124,6 @@ p[find(a)] = find(b)
122124
d[find(a)] = distance
123125
```
124126

125-
对于本题,先遍历所有的等式,构造并查集。接着遍历所有不等式,如果不等式的两个变量处于同一个集合,说明发生矛盾,返回 false。否则遍历结束返回 true。
126-
127127
<!-- tabs:start -->
128128

129129
### **Python3**
@@ -133,20 +133,19 @@ d[find(a)] = distance
133133
```python
134134
class Solution:
135135
def equationsPossible(self, equations: List[str]) -> bool:
136-
p = [i for i in range(26)]
137-
138136
def find(x):
139137
if p[x] != x:
140138
p[x] = find(p[x])
141139
return p[x]
142140

141+
p = list(range(26))
143142
for e in equations:
144-
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
145-
if r == '==':
143+
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
144+
if e[1] == '=':
146145
p[find(a)] = find(b)
147146
for e in equations:
148-
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
149-
if r == '!=' and find(a) == find(b):
147+
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
148+
if e[1] == '!' and find(a) == find(b):
150149
return False
151150
return True
152151
```
@@ -166,15 +165,13 @@ class Solution {
166165
}
167166
for (String e : equations) {
168167
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
169-
String r = e.substring(1, 3);
170-
if ("==".equals(r)) {
168+
if (e.charAt(1) == '=') {
171169
p[find(a)] = find(b);
172170
}
173171
}
174172
for (String e : equations) {
175173
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
176-
String r = e.substring(1, 3);
177-
if ("!=".equals(r) && find(a) == find(b)) {
174+
if (e.charAt(1) == '!' && find(a) == find(b)) {
178175
return false;
179176
}
180177
}
@@ -199,28 +196,22 @@ public:
199196

200197
bool equationsPossible(vector<string>& equations) {
201198
p.resize(26);
202-
for (int i = 0; i < 26; ++i)
203-
p[i] = i;
204-
for (auto e : equations)
199+
for (int i = 0; i < 26; ++i) p[i] = i;
200+
for (auto& e : equations)
205201
{
206202
int a = e[0] - 'a', b = e[3] - 'a';
207-
char r = e[1];
208-
if (r == '=')
209-
p[find(a)] = find(b);
203+
if (e[1] == '=') p[find(a)] = find(b);
210204
}
211-
for (auto e : equations)
205+
for (auto& e : equations)
212206
{
213207
int a = e[0] - 'a', b = e[3] - 'a';
214-
char r = e[1];
215-
if (r == '!' && find(a) == find(b))
216-
return false;
208+
if (e[1] == '!' && find(a) == find(b)) return false;
217209
}
218210
return true;
219211
}
220212

221213
int find(int x) {
222-
if (p[x] != x)
223-
p[x] = find(p[x]);
214+
if (p[x] != x) p[x] = find(p[x]);
224215
return p[x];
225216
}
226217
};
@@ -229,36 +220,32 @@ public:
229220
### **Go**
230221

231222
```go
232-
var p []int
233-
234223
func equationsPossible(equations []string) bool {
235-
p = make([]int, 26)
224+
p := make([]int, 26)
236225
for i := 1; i < 26; i++ {
237226
p[i] = i
238227
}
228+
var find func(x int) int
229+
find = func(x int) int {
230+
if p[x] != x {
231+
p[x] = find(p[x])
232+
}
233+
return p[x]
234+
}
239235
for _, e := range equations {
240236
a, b := int(e[0]-'a'), int(e[3]-'a')
241-
r := e[1]
242-
if r == '=' {
237+
if e[1] == '=' {
243238
p[find(a)] = find(b)
244239
}
245240
}
246241
for _, e := range equations {
247242
a, b := int(e[0]-'a'), int(e[3]-'a')
248-
r := e[1]
249-
if r == '!' && find(a) == find(b) {
243+
if e[1] == '!' && find(a) == find(b) {
250244
return false
251245
}
252246
}
253247
return true
254248
}
255-
256-
func find(x int) int {
257-
if p[x] != x {
258-
p[x] = find(p[x])
259-
}
260-
return p[x]
261-
}
262249
```
263250

264251
### **...**

solution/0900-0999/0990.Satisfiability of Equality Equations/README_EN.md

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,28 @@
102102

103103
## Solutions
104104

105+
Union find.
106+
105107
<!-- tabs:start -->
106108

107109
### **Python3**
108110

109111
```python
110112
class Solution:
111113
def equationsPossible(self, equations: List[str]) -> bool:
112-
p = [i for i in range(26)]
113-
114114
def find(x):
115115
if p[x] != x:
116116
p[x] = find(p[x])
117117
return p[x]
118118

119+
p = list(range(26))
119120
for e in equations:
120-
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
121-
if r == '==':
121+
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
122+
if e[1] == '=':
122123
p[find(a)] = find(b)
123124
for e in equations:
124-
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
125-
if r == '!=' and find(a) == find(b):
125+
a, b = ord(e[0]) - ord('a'), ord(e[-1]) - ord('a')
126+
if e[1] == '!' and find(a) == find(b):
126127
return False
127128
return True
128129
```
@@ -140,15 +141,13 @@ class Solution {
140141
}
141142
for (String e : equations) {
142143
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
143-
String r = e.substring(1, 3);
144-
if ("==".equals(r)) {
144+
if (e.charAt(1) == '=') {
145145
p[find(a)] = find(b);
146146
}
147147
}
148148
for (String e : equations) {
149149
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
150-
String r = e.substring(1, 3);
151-
if ("!=".equals(r) && find(a) == find(b)) {
150+
if (e.charAt(1) == '!' && find(a) == find(b)) {
152151
return false;
153152
}
154153
}
@@ -173,28 +172,22 @@ public:
173172

174173
bool equationsPossible(vector<string>& equations) {
175174
p.resize(26);
176-
for (int i = 0; i < 26; ++i)
177-
p[i] = i;
178-
for (auto e : equations)
175+
for (int i = 0; i < 26; ++i) p[i] = i;
176+
for (auto& e : equations)
179177
{
180178
int a = e[0] - 'a', b = e[3] - 'a';
181-
char r = e[1];
182-
if (r == '=')
183-
p[find(a)] = find(b);
179+
if (e[1] == '=') p[find(a)] = find(b);
184180
}
185-
for (auto e : equations)
181+
for (auto& e : equations)
186182
{
187183
int a = e[0] - 'a', b = e[3] - 'a';
188-
char r = e[1];
189-
if (r == '!' && find(a) == find(b))
190-
return false;
184+
if (e[1] == '!' && find(a) == find(b)) return false;
191185
}
192186
return true;
193187
}
194188

195189
int find(int x) {
196-
if (p[x] != x)
197-
p[x] = find(p[x]);
190+
if (p[x] != x) p[x] = find(p[x]);
198191
return p[x];
199192
}
200193
};
@@ -203,36 +196,32 @@ public:
203196
### **Go**
204197

205198
```go
206-
var p []int
207-
208199
func equationsPossible(equations []string) bool {
209-
p = make([]int, 26)
200+
p := make([]int, 26)
210201
for i := 1; i < 26; i++ {
211202
p[i] = i
212203
}
204+
var find func(x int) int
205+
find = func(x int) int {
206+
if p[x] != x {
207+
p[x] = find(p[x])
208+
}
209+
return p[x]
210+
}
213211
for _, e := range equations {
214212
a, b := int(e[0]-'a'), int(e[3]-'a')
215-
r := e[1]
216-
if r == '=' {
213+
if e[1] == '=' {
217214
p[find(a)] = find(b)
218215
}
219216
}
220217
for _, e := range equations {
221218
a, b := int(e[0]-'a'), int(e[3]-'a')
222-
r := e[1]
223-
if r == '!' && find(a) == find(b) {
219+
if e[1] == '!' && find(a) == find(b) {
224220
return false
225221
}
226222
}
227223
return true
228224
}
229-
230-
func find(x int) int {
231-
if p[x] != x {
232-
p[x] = find(p[x])
233-
}
234-
return p[x]
235-
}
236225
```
237226

238227
### **...**

solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,22 @@ class Solution {
44

55
bool equationsPossible(vector<string>& equations) {
66
p.resize(26);
7-
for (int i = 0; i < 26; ++i)
8-
p[i] = i;
9-
for (auto e : equations)
7+
for (int i = 0; i < 26; ++i) p[i] = i;
8+
for (auto& e : equations)
109
{
1110
int a = e[0] - 'a', b = e[3] - 'a';
12-
char r = e[1];
13-
if (r == '=')
14-
p[find(a)] = find(b);
11+
if (e[1] == '=') p[find(a)] = find(b);
1512
}
16-
for (auto e : equations)
13+
for (auto& e : equations)
1714
{
1815
int a = e[0] - 'a', b = e[3] - 'a';
19-
char r = e[1];
20-
if (r == '!' && find(a) == find(b))
21-
return false;
16+
if (e[1] == '!' && find(a) == find(b)) return false;
2217
}
2318
return true;
2419
}
2520

2621
int find(int x) {
27-
if (p[x] != x)
28-
p[x] = find(p[x]);
22+
if (p[x] != x) p[x] = find(p[x]);
2923
return p[x];
3024
}
3125
};
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
var p []int
2-
31
func equationsPossible(equations []string) bool {
4-
p = make([]int, 26)
2+
p := make([]int, 26)
53
for i := 1; i < 26; i++ {
64
p[i] = i
75
}
6+
var find func(x int) int
7+
find = func(x int) int {
8+
if p[x] != x {
9+
p[x] = find(p[x])
10+
}
11+
return p[x]
12+
}
813
for _, e := range equations {
914
a, b := int(e[0]-'a'), int(e[3]-'a')
10-
r := e[1]
11-
if r == '=' {
15+
if e[1] == '=' {
1216
p[find(a)] = find(b)
1317
}
1418
}
1519
for _, e := range equations {
1620
a, b := int(e[0]-'a'), int(e[3]-'a')
17-
r := e[1]
18-
if r == '!' && find(a) == find(b) {
21+
if e[1] == '!' && find(a) == find(b) {
1922
return false
2023
}
2124
}
2225
return true
23-
}
24-
25-
func find(x int) int {
26-
if p[x] != x {
27-
p[x] = find(p[x])
28-
}
29-
return p[x]
3026
}

solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ public boolean equationsPossible(String[] equations) {
88
}
99
for (String e : equations) {
1010
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
11-
String r = e.substring(1, 3);
12-
if ("==".equals(r)) {
11+
if (e.charAt(1) == '=') {
1312
p[find(a)] = find(b);
1413
}
1514
}
1615
for (String e : equations) {
1716
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
18-
String r = e.substring(1, 3);
19-
if ("!=".equals(r) && find(a) == find(b)) {
17+
if (e.charAt(1) == '!' && find(a) == find(b)) {
2018
return false;
2119
}
2220
}

0 commit comments

Comments
 (0)