Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 761336b

Browse files
committed
feat: add solutions to lc problem: No.0990.Satisfiability of Equality Equations
1 parent 87fb929 commit 761336b

File tree

6 files changed

+405
-3
lines changed

6 files changed

+405
-3
lines changed

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

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,199 @@
5959
<li><code>equations[i][2]</code>&nbsp;是&nbsp;<code>&#39;=&#39;</code></li>
6060
</ol>
6161

62-
6362
## 解法
6463

6564
<!-- 这里可写通用的实现逻辑 -->
6665

66+
并查集。
67+
68+
模板 1——朴素并查集:
69+
70+
```python
71+
# 初始化,p存储每个点的祖宗节点
72+
p = [i for i in range(n)]
73+
# 返回x的祖宗节点
74+
def find(x):
75+
if p[x] != x:
76+
# 路径压缩
77+
p[x] = find(p[x])
78+
return p[x]
79+
# 合并a和b所在的两个集合
80+
p[find(a)] = find(b)
81+
```
82+
83+
模板 2——维护 size 的并查集:
84+
85+
```python
86+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
87+
p = [i for i in range(n)]
88+
size = [1] * n
89+
# 返回x的祖宗节点
90+
def find(x):
91+
if p[x] != x:
92+
# 路径压缩
93+
p[x] = find(p[x])
94+
return p[x]
95+
# 合并a和b所在的两个集合
96+
size[find(b)] += size[find(a)]
97+
p[find(a)] = find(b)
98+
```
99+
100+
模板 3——维护到祖宗节点距离的并查集:
101+
102+
```python
103+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
104+
p = [i for i in range(n)]
105+
d = [0] * n
106+
# 返回x的祖宗节点
107+
def find(x):
108+
if p[x] != x:
109+
t = find(p[x])
110+
d[x] += d[p[x]]
111+
p[x] = t
112+
return p[x]
113+
# 合并a和b所在的两个集合
114+
p[find(a)] = find(b)
115+
d[find(a)] = dinstance
116+
```
117+
118+
对于本题,先遍历所有的等式,构造并查集。接着遍历所有不等式,如果不等式的两个变量处于同一个集合,说明发生矛盾,返回 false。否则遍历结束返回 true。
119+
67120
<!-- tabs:start -->
68121

69122
### **Python3**
70123

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

73126
```python
74-
127+
class Solution:
128+
def equationsPossible(self, equations: List[str]) -> bool:
129+
p = [i for i in range(26)]
130+
131+
def find(x):
132+
if p[x] != x:
133+
p[x] = find(p[x])
134+
return p[x]
135+
136+
for e in equations:
137+
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
138+
if r == '==':
139+
p[find(a)] = find(b)
140+
for e in equations:
141+
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
142+
if r == '!=' and find(a) == find(b):
143+
return False
144+
return True
75145
```
76146

77147
### **Java**
78148

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

81151
```java
152+
class Solution {
153+
private int[] p;
154+
155+
public boolean equationsPossible(String[] equations) {
156+
p = new int[26];
157+
for (int i = 0; i < 26; ++i) {
158+
p[i] = i;
159+
}
160+
for (String e : equations) {
161+
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
162+
String r = e.substring(1, 3);
163+
if ("==".equals(r)) {
164+
p[find(a)] = find(b);
165+
}
166+
}
167+
for (String e : equations) {
168+
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
169+
String r = e.substring(1, 3);
170+
if ("!=".equals(r) && find(a) == find(b)) {
171+
return false;
172+
}
173+
}
174+
return true;
175+
}
176+
177+
private int find(int x) {
178+
if (p[x] != x) {
179+
p[x] = find(p[x]);
180+
}
181+
return p[x];
182+
}
183+
}
184+
```
185+
186+
### **C++**
187+
188+
```cpp
189+
class Solution {
190+
public:
191+
vector<int> p;
192+
193+
bool equationsPossible(vector<string>& equations) {
194+
p.resize(26);
195+
for (int i = 0; i < 26; ++i)
196+
p[i] = i;
197+
for (auto e : equations)
198+
{
199+
int a = e[0] - 'a', b = e[3] - 'a';
200+
char r = e[1];
201+
if (r == '=')
202+
p[find(a)] = find(b);
203+
}
204+
for (auto e : equations)
205+
{
206+
int a = e[0] - 'a', b = e[3] - 'a';
207+
char r = e[1];
208+
if (r == '!' && find(a) == find(b))
209+
return false;
210+
}
211+
return true;
212+
}
213+
214+
int find(int x) {
215+
if (p[x] != x)
216+
p[x] = find(p[x]);
217+
return p[x];
218+
}
219+
};
220+
```
82221

222+
### **Go**
223+
224+
```go
225+
var p []int
226+
227+
func equationsPossible(equations []string) bool {
228+
p = make([]int, 26)
229+
for i := 1; i < 26; i++ {
230+
p[i] = i
231+
}
232+
for _, e := range equations {
233+
a, b := int(e[0]-'a'), int(e[3]-'a')
234+
r := e[1]
235+
if r == '=' {
236+
p[find(a)] = find(b)
237+
}
238+
}
239+
for _, e := range equations {
240+
a, b := int(e[0]-'a'), int(e[3]-'a')
241+
r := e[1]
242+
if r == '!' && find(a) == find(b) {
243+
return false
244+
}
245+
}
246+
return true
247+
}
248+
249+
func find(x int) int {
250+
if p[x] != x {
251+
p[x] = find(p[x])
252+
}
253+
return p[x]
254+
}
83255
```
84256

85257
### **...**

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

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,132 @@
141141
### **Python3**
142142

143143
```python
144-
144+
class Solution:
145+
def equationsPossible(self, equations: List[str]) -> bool:
146+
p = [i for i in range(26)]
147+
148+
def find(x):
149+
if p[x] != x:
150+
p[x] = find(p[x])
151+
return p[x]
152+
153+
for e in equations:
154+
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
155+
if r == '==':
156+
p[find(a)] = find(b)
157+
for e in equations:
158+
a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
159+
if r == '!=' and find(a) == find(b):
160+
return False
161+
return True
145162
```
146163

147164
### **Java**
148165

149166
```java
167+
class Solution {
168+
private int[] p;
169+
170+
public boolean equationsPossible(String[] equations) {
171+
p = new int[26];
172+
for (int i = 0; i < 26; ++i) {
173+
p[i] = i;
174+
}
175+
for (String e : equations) {
176+
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
177+
String r = e.substring(1, 3);
178+
if ("==".equals(r)) {
179+
p[find(a)] = find(b);
180+
}
181+
}
182+
for (String e : equations) {
183+
int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
184+
String r = e.substring(1, 3);
185+
if ("!=".equals(r) && find(a) == find(b)) {
186+
return false;
187+
}
188+
}
189+
return true;
190+
}
191+
192+
private int find(int x) {
193+
if (p[x] != x) {
194+
p[x] = find(p[x]);
195+
}
196+
return p[x];
197+
}
198+
}
199+
```
200+
201+
### **C++**
202+
203+
```cpp
204+
class Solution {
205+
public:
206+
vector<int> p;
207+
208+
bool equationsPossible(vector<string>& equations) {
209+
p.resize(26);
210+
for (int i = 0; i < 26; ++i)
211+
p[i] = i;
212+
for (auto e : equations)
213+
{
214+
int a = e[0] - 'a', b = e[3] - 'a';
215+
char r = e[1];
216+
if (r == '=')
217+
p[find(a)] = find(b);
218+
}
219+
for (auto e : equations)
220+
{
221+
int a = e[0] - 'a', b = e[3] - 'a';
222+
char r = e[1];
223+
if (r == '!' && find(a) == find(b))
224+
return false;
225+
}
226+
return true;
227+
}
228+
229+
int find(int x) {
230+
if (p[x] != x)
231+
p[x] = find(p[x]);
232+
return p[x];
233+
}
234+
};
235+
```
150236

237+
### **Go**
238+
239+
```go
240+
var p []int
241+
242+
func equationsPossible(equations []string) bool {
243+
p = make([]int, 26)
244+
for i := 1; i < 26; i++ {
245+
p[i] = i
246+
}
247+
for _, e := range equations {
248+
a, b := int(e[0]-'a'), int(e[3]-'a')
249+
r := e[1]
250+
if r == '=' {
251+
p[find(a)] = find(b)
252+
}
253+
}
254+
for _, e := range equations {
255+
a, b := int(e[0]-'a'), int(e[3]-'a')
256+
r := e[1]
257+
if r == '!' && find(a) == find(b) {
258+
return false
259+
}
260+
}
261+
return true
262+
}
263+
264+
func find(x int) int {
265+
if p[x] != x {
266+
p[x] = find(p[x])
267+
}
268+
return p[x]
269+
}
151270
```
152271

153272
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
5+
bool equationsPossible(vector<string>& equations) {
6+
p.resize(26);
7+
for (int i = 0; i < 26; ++i)
8+
p[i] = i;
9+
for (auto e : equations)
10+
{
11+
int a = e[0] - 'a', b = e[3] - 'a';
12+
char r = e[1];
13+
if (r == '=')
14+
p[find(a)] = find(b);
15+
}
16+
for (auto e : equations)
17+
{
18+
int a = e[0] - 'a', b = e[3] - 'a';
19+
char r = e[1];
20+
if (r == '!' && find(a) == find(b))
21+
return false;
22+
}
23+
return true;
24+
}
25+
26+
int find(int x) {
27+
if (p[x] != x)
28+
p[x] = find(p[x]);
29+
return p[x];
30+
}
31+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /