leetcode990

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode990相关的知识,希望对你有一定的参考价值。

 1 class Finder:
 2     def __init__(self):
 3         self.Parent = [i for i in range(26)]
 4     def union(self, p, q):
 5         self.Parent[self.find(p)] =self.find(q)
 6     
 7     def find(self, p):
 8         if self.Parent[p] != p:
 9             return self.find(self.Parent[p])
10         else:
11             return p
12 class Solution(object):
13     def equationsPossible(self, equations):
14         """
15         :type equations: List[str]
16         :rtype: bool
17         """
18         def convert(c):
19             return ord(c)-ord(a)
20         
21         finder = Finder()
22         for e in equations:
23             if e.find("==") == 1:
24                 left, right = convert(e[0]), convert(e[3])
25                 finder.union(left, right)
26             else:
27                 pass
28         for e in equations:
29             if e.find("!=") == 1:
30                 left, right = convert(e[0]), convert(e[3])
31                 if finder.find(left) == finder.find(right):
32                     return False
33         return True

并查集方案

以上是关于leetcode990的主要内容,如果未能解决你的问题,请参考以下文章

leetcode990

leetcode每日一题(2020-06-07):990. 等式方程的可满足性

leetcode每日一题(2020-06-07):990. 等式方程的可满足性

LeetCode1707. 与数组中元素的最大异或值 (字典树)/ 990. 等式方程的可满足性(并查集)

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

CF990G