【leetcode】990. Satisfiability of Equality Equations
题目如下:
Given an array equations of strings that represent relationships between variables, each string
equations[i]
has length4
and takes one of two different forms:"a==b"
or"a!=b"
. Here,a
andb
are lowercase letters (not necessarily different) that represent one-letter variable names.Return
true
if 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 <= 500
equations[i].length == 4
equations[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 ...
随机推荐
- Ubunu12.04 vncserver xstartup配置文件
以下有效: #!/bin/sh # Uncomment the following two lines for normal desktop: unset SESSION_MANAGER #exec ...
- macOS系统安装gnuplot(解决Terminal type set to unknown)
macOS 下使用 Homebrew 安装 gnuplot brew install gnuplot 在 terminal 中输入gnuplot进入模式之后,提示_Terminal type set ...
- Linux 多个cpp文件的编译(Makefile)
打包so文件: CC = g++ CFLAGS=-Wall -O2 -fPIC TARGET = libbg.so SRCS := $(wildcard *.cpp) OBJS := $(patsub ...
- python如何在shell命令行执行创建用户命令
- 如何在某个apps包下面中创建APP
- FMX Android ZIP解压中文乱码
在手机上解压了一个WINDOWS上的压缩文件, 发现中文是乱码的,解决方法如下: 找到System.zip.pas文件 将E := TEncoding.GetEncoding(437); 改为 E ...
- MVVM MVC
在WPF的MVVM模式中,View和ViewModel之间数据和命令的关联都是通过绑定实现的,绑定后View和ViewModel并不产生直接的依赖.具体就是View中出现数据变化时会尝试修改绑定的目标 ...
- C#正则表达式将html代码中的所有img标签提取
/// <summary> /// 取得HTML中所有图片的 URL. /// </summary> /// <param name="sHtmlText&qu ...
- SQL学习记录:函数(二)
字符串函数 1.获取字符的ASCII码 语法结构: ASCII(espression) 这里的expression是一个返回char或varchar数据类型的表达式,ASCII函数仅对表达式最左 ...
- python分类预测模型的特点
python分类预测模型的特点 模型 模型特点 位于 SVM 强大的模型,可以用来回归,预测,分类等,而根据选取不同的和函数,模型可以是线性的/非线性的 sklearn.svm 决策树 基于" ...