leetcode990
class Finder:
def __init__(self):
self.Parent = [i for i in range(26)]
def union(self, p, q):
self.Parent[self.find(p)] =self.find(q) def find(self, p):
if self.Parent[p] != p:
return self.find(self.Parent[p])
else:
return p
class Solution(object):
def equationsPossible(self, equations):
"""
:type equations: List[str]
:rtype: bool
"""
def convert(c):
return ord(c)-ord('a') finder = Finder()
for e in equations:
if e.find("==") == 1:
left, right = convert(e[0]), convert(e[3])
finder.union(left, right)
else:
pass
for e in equations:
if e.find("!=") == 1:
left, right = convert(e[0]), convert(e[3])
if finder.find(left) == finder.find(right):
return False
return True
并查集方案
leetcode990的更多相关文章
- [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations
Given an array equations of strings that represent relationships between variables, each string equa ...
- 并查集算法Union-Find的思想、实现以及应用
并查集算法,也叫Union-Find算法,主要用于解决图论中的动态连通性问题. Union-Find算法类 这里直接给出并查集算法类UnionFind.class,如下: /** * Union-Fi ...
随机推荐
- Spring mvc Json 的正确返回姿势
我们经常都需要封装统一JSON格式 例如以下形式 { “data”:“输出的数据”, “code”:“响应代码”, “msg”:“响应信息” } /** * Created by linli on 2 ...
- Ubuntu16.04 创建桌面快捷方式
一.基本概念 Linux 系统中的Desktop Entry 文件以desktop为后缀名.Desktop Entry 文件是 Linux 桌面系统中用于描述程序启动配置信息的文件. 进入/usr/s ...
- python学习疑问
1.(已解决) test = [1, 2, 3, 4] ", id(test)) def func(a): ", id(a)) a = a.remove(1) ", id ...
- QT中实现应用程序的单例化
一介绍 通过编写一个QSingleApplication类,来实现Qt程序的单例化,原文的作者是在Windows Vista + Qt4.4 下实现的,不过应用在其他平台上是没问题的.(本文是我在ht ...
- OpenSSH多路复用Multiplexing配置
设置 Session Multiplexing 在客户端节点如下配置/etc/ssh/ssh_config 或~/.ssh/config 就可以直接开启 Session Multiplexing 功能 ...
- script中type属性讲解
js的代码是由type决定的: <script type='javascript'> 默认的 <script type="text/html" > 就是 ...
- grafana 指标视图嵌入到其他html网页
我们开发了一套管理平台用来监控整个系统环境的运行情况,但是在指标信息这块不想重新开发,而想直接拿grafana来用,刚开始的时候我们的管理平台和grafana是完全独立的,只能从我们平台跳转到graf ...
- js对象-平铺与嵌套的互相转换
一个json对象,包含嵌套关系,传输过来的时候是平铺的,顺序打乱,用parentCode属性来关联,如下 { "1":{ "name": "中国&qu ...
- 知识点:linux数据库备份
服务端启用二进制日志 如果日志没有启开,必须启用binlog,要重启mysql,首先,关闭mysql,打开/etc/my.cnf,加入以下几行: [mysqld] log-bin 然后重新启动mysq ...
- tips:Java中的switch的选择因子
tips:Java中的switch的选择因子! /* switch(){ } */ switch的()中的判断条件能是什么类型呢? 事实上,在我们学习c++的了解中,switch的参数是一个能 ...