题目如下:

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. PLSQL连接oracle12c

    一.本人以前都是使用oracle10g客户端,PLSQL连接oracle12c时报错 确认配置完全没问题,纠结了不少时间.后来查的是oracle客户端太老了,版本11.2.0.2.0以上即可 二.下载 ...

  2. 每天一个linux命令:more(13)

    more more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻 ...

  3. Adobe Flash Player30.0.0.113离线安装包

    远程控制工具  www.iis7.net/a/lm/yczmljgj/ Adobe2017年7月26日宣布,计划终结 Flash 浏览器插件,并在2020年停止开发和分发这款插件.Adobe 建议内容 ...

  4. jenkins的安装与使用

    以前用过hudson,前段时间听以前同事说,他现在搞jenkins,zookeeper...,现在的项目 也是手动的,所以我也就搞了一个jenkins.期间也遇到好多问题,主要是自己水平不够,网上的都 ...

  5. 解决“element表单验证输入的数字检测出来是string”的问题

    form表单: 校验规则: 注意:一.数字类型的验证需要在 v-model 处加上 .number 的修饰符,这是 Vue 自身提供的用于将绑定值转化为 number 类型的修饰符.二.校验中是否添加 ...

  6. 渗透测试工具sqlmap基础教程

    转载请注明出处:http://blog.csdn.net/zgyulongfei/article/details/41017493 作者:羽龍飛 本文仅献给想学习渗透测试的sqlmap小白,大牛请绕过 ...

  7. 6105 - deauth after EAPOL key exchange sequence

    wifi无法连接公司的网络 Warning Error in Event Log - deauth after EAPOL key exchange sequence https://forums.i ...

  8. JetBrains CLion

    JetBrains CLion 2017.2.4 ①.激活时选择License server: http://idea.irfen.me/ http://idea.imsxm.com/

  9. 在RedHat中安装新字体

    安装 下载这个字体. http://pan.baidu.com/s/1c23znaS 密码:tldo 在/usr/share/fonts/truetype/, 下建立一个新的目录 YaHei Cons ...

  10. jmeter添加自定义扩展函数之String---base64加密

    1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...