【leetcode】990. Satisfiability of Equality Equations
题目如下:
Given an array equations of strings that represent relationships between variables, each string
equations[i]has length4and takes one of two different forms:"a==b"or"a!=b". Here,aandbare lowercase letters (not necessarily different) that represent one-letter variable names.Return
trueif and only if it is possible to assign integers to variable names so as to satisfy all the given equations.Example 1:
Input: ["a==b","b!=a"]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.Example 2:
Input: ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.Example 3:
Input: ["a==b","b==c","a==c"]
Output: trueExample 4:
Input: ["a==b","b!=c","c==a"]
Output: falseExample 5:
Input: ["c==c","b==d","x!=z"]
Output: trueNote:
1 <= equations.length <= 500equations[i].length == 4equations[i][0]andequations[i][3]are lowercase lettersequations[i][1]is either'='or'!'equations[i][2]is'='
解题思路:我的方法是用并查集,先把所有等式中的字母合并组成并查集,接下来再判断不等式中的字母是否属于同一祖先。
代码如下:
class Solution(object):
def equationsPossible(self, equations):
"""
:type equations: List[str]
:rtype: bool
"""
def find(v):
if parent[v] == v:
return v
return find(parent[v]) def union(v1,v2):
p1 = find(v1)
p2 = find(v2)
if p1 < p2:
parent[p2] = p1
else:
parent[p1] = p2
parent = [i for i in range(26)] uneuqal = []
while len(equations) > 0:
item = equations.pop(0)
if item[1] == '!':
uneuqal.append(item)
else:
union(ord(item[0]) - ord('a'),ord(item[3]) - ord('a')) for item in uneuqal:
if find(ord(item[0]) - ord('a')) == find(ord(item[3]) - ord('a')):
return False
return True
【leetcode】990. Satisfiability of Equality Equations的更多相关文章
- 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...
- 【medium】990. Satisfiability of Equality Equations 并查集
Given an array equations of strings that represent relationships between variables, each string equa ...
- LC 990. Satisfiability of Equality Equations
Given an array equations of strings that represent relationships between variables, each string equa ...
- LeetCode 990. Satisfiability of Equality Equations
原题链接在这里:https://leetcode.com/problems/satisfiability-of-equality-equations/ 题目: Given an array equat ...
- Satisfiability of Equality Equations - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Satisfiability of Equality Equations - LeetCode 注意点 必须要初始化pre 解法 解法一:典型的并查集算法 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- 51nod 1605:棋盘问题
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1605 题目中最有用的点其实还是x必为奇数 #include& ...
- JS中数据结构之集合
集合(set)是一种包含不同元素的数据结构.集合中的元素称为成员.集合的两个最重要特性是:首先,集合中的成员是无序的:其次,集合中不允许相同成员存在.当你想要创建一个数据结构用来保存一些独一无二的元素 ...
- LOJ 3058 「HNOI2019」白兔之舞——单位根反演+MTT
题目:https://loj.ac/problem/3058 先考虑 n=1 怎么做.令 a 表示输入的 w[1][1] . \( ans_t = \sum\limits_{i=0}^{L}C_{L} ...
- [CSP-S模拟测试]:u(差分)
题目背景 $\frac{1}{4}$遇到了一道水题,完全不会做,于是去请教小$D$.小$D$看了一眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{4}$只好来问 ...
- Django中的get()和filter()区别
前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...
- hdu 5517 Triple
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5517 ------------------------------------------------ ...
- LINUX shell脚本相关
调试脚本 测试脚本语法:bash -n file.sh 查看脚本每一步执行情况:bash -x file.sh 位置变量:$1,$2,... 特殊变量: %?:最后一个命令的执 ...
- 测开之路三十三:Flask实现扎金花游戏
访问http://localhost:8888/game随机获取一张扑克牌.豹子(炸弹):三张点相同的牌.例:AAA.222.顺金(同花顺.色托):花色相同的顺子.例:黑桃456.红桃789.最大的顺 ...
- Maximum of lines in a DataBand
Hello! I have a problem.I have a DataBand, but I need it to grow only up to 14 lines. If it is beyon ...
- Html5 学习笔记 【PC固定布局】 实战5 咨询页面 侧栏
最终效果图: 提出公共页脚和导航部分: 新建infomatino.html (旅游咨询)页面 <!DOCTYPE html> <html lang="zh-cn" ...