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 ...
随机推荐
- 关于nginx大流量负载调优
优化nginx包括两方面: 1.是自己重写nginx代码(比如tengine).本身nginx的代码已经足够优秀,如果不是每秒几千的请求,就忽略这个部分吧. 2.另一个就是和优化nginx的配置,这是 ...
- ALGO-140_蓝桥杯_算法训练_P1101
有一份提货单,其数据项目有:商品名(MC).单价(DJ).数量(SL).定义一个结构体prut,其成员是上面的三项数据.在主函数中定义一个prut类型的结构体数组,输入每个元素的值,计算并输出提货单的 ...
- P1601高精度加法
传送门 虽然本题一本通上有,但是一本通不是万能的,这道题就漏掉了进位(所以这告诉我们加法进位很重要) 直接上修改后的题解 #include<iostream> #include<cs ...
- spring4.0之三:@RestController
spring4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个 ...
- springboot(五)读写分离,多个读库,Druid监控--待整理
1.修改mybatis.properties # 主数据源,默认的 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.d ...
- dropwizard使用cors支持跨域浏览器取不到自定义header问题
dropwizard支持cors的配置如下: public void run(Configuration conf, Environment environment) { // Enable CORS ...
- vultr 上实现高可用冗余浮动公网IP出口(使用BIRD+BGP协议)High Availability on Vultr with Floating IP and BGP
官方文档: https://www.vultr.com/docs/high-availability-on-vultr-with-floating-ip-and-bgp https://www.vul ...
- VMware同时使用三种网络模式的虚拟机,测试连通性
参考资料: 虚拟机中桥接不自动分配IP与设置静态IP问题: https://blog.csdn.net/u013187057/article/details/80579211 腾讯课堂: https: ...
- sqlserver基本增删查语句
use StudentManageDB go insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo ,PhoneNumbe ...
- Linux开机自动挂载windows网络共享
yum install samba-client yum install cifs.utils yum install samba-common 命令: mount -v -t cifs // ...