题目如下:

Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b".  Here, a and bare 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: true

Example 4:

Input: ["a==b","b!=c","c==a"]
Output: false

Example 5:

Input: ["c==c","b==d","x!=z"]
Output: true

Note:

  1. 1 <= equations.length <= 500
  2. equations[i].length == 4
  3. equations[i][0] and equations[i][3] are lowercase letters
  4. equations[i][1] is either '=' or '!'
  5. 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的更多相关文章

  1. 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...

  2. 【medium】990. Satisfiability of Equality Equations 并查集

    Given an array equations of strings that represent relationships between variables, each string equa ...

  3. LC 990. Satisfiability of Equality Equations

    Given an array equations of strings that represent relationships between variables, each string equa ...

  4. LeetCode 990. Satisfiability of Equality Equations

    原题链接在这里:https://leetcode.com/problems/satisfiability-of-equality-equations/ 题目: Given an array equat ...

  5. Satisfiability of Equality Equations - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Satisfiability of Equality Equations - LeetCode 注意点 必须要初始化pre 解法 解法一:典型的并查集算法 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. onkeyup的使用(将输入值为非数字的字符替换为空)

    onkeyup:当输入值的键盘抬起时触发这个事件. 例如: onkeyup="this.value=this.value.replace(/\D/g,'') 这是个正则式验证,用来验证输入值 ...

  2. uiautomator python版本

    摘要: 利用jsonrpc技术直接包装uiautomator,调用uiautomator的api在本地直接编写脚本,然后运行测试case,这样比用Java写,然后再打包,runcase要简单的多,关键 ...

  3. 多线程模拟生产者消费者示例之Lock

    public class Test { public static void main(String[] args) { List<String> list = new ArrayList ...

  4. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  5. IPC机制总结

    IPC机制: 1. 多进程概念: 2. 序列化机制和Binder: 3. 进程间通信:Bundle.文件共享.AIDL.Messenger.ContentProvider.Socket: 操作系统的设 ...

  6. rabbitmqctl常用命令-3

    1)启动.关闭 rabbitmq节点和应用 rabbitmq-server -detached #rabbitmq分别启动节点和应用 应用关闭rabbitmqctl stop_app 应用启动 rab ...

  7. 2018-2019-20175203 实验二 《Java面向对象程序设计》

    2018-2019-20175203 实验二 <Java面向对象程序设计>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑 ...

  8. LINUX 文件合并,去重

    (1)两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)cat file1 file2 | sort | uniq > file32. 取出两个文 ...

  9. delphi vlc 安装bug 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"

    处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" [摘要:http://blog.csdn ...

  10. flask扩展系列之 - 访问速度限制

    flask-limiter 是一个对客户端的访问速率进行限制的flask扩展.可以自定义一些访问的(速度)限制条件来把那些触发限制的请求拒之门外.一般常用来进行对爬虫的限制. 下面就常见的用法,举了一 ...