leetcode 每日一题 https://leetcode-cn.com/problems/satisfiability-of-equality-equations/
思路:并查集 先把所有==合并,再将所有 !=查找。如果是同一个父亲就return false 。
class Solution: def equationsPossible(self, equations: 'List[str]') -> 'bool': fa={chr(i):chr(i) for i in range(97,125)} def find(x): if x!= fa[x]: fa[x] = find(fa[x]) return fa[x] for it in equations: if(it[1]=="="): fa[find(it[0])]=find(it[3]) for it in equations: if (it[1] == "!"): if(find(it[3])==find(it[0])): return False return True