990. 等式方程的可满足性

Medium

思路

首先还是一样,阅读理解 先看等式,a==b , b==c 从此,我们可以得到隐藏的条件a == c,这时假如数组中有a != c,那就是矛盾的。

综上,AC!

代码

python3

class Solution:
    def equationsPossible(self, equations: List[str]) -> bool:
      class UnionFind():
        def __init__(self, equations):
          self.s = {}
          for e in equations:
            self.s[e[0]] = e[0]
            self.s[e[3]] = e[3]
        
        def find(self, x):
          while x != self.s[x]:
            x = self.s[x]
          return x

        def union(self, a ,b):
          if self.find(a) != self.find(b):
            self.s[self.find(a)] = self.find(b)

      uf = UnionFind(equations)
      for e in equations:
        if e[1] != "!":
          uf.union(e[0], e[3])
      for e in equations:
        if e[1] == "!":
          if uf.find(e[0]) == uf.find(e[3]):
            return False
      return True