【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 ...
随机推荐
- 媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
如今,双11不光是购物狂欢节,更是对技术的一次“大考”,对于阿里巴巴企业内部运营的基础保障技术而言,亦是如此. 回溯双11历史,这背后也经历过“小米加步枪”的阶段:作战室从随处是网线,交换机放地上的“ ...
- 趣头条基于 Flink 的实时平台建设实践
本文由趣头条实时平台负责人席建刚分享趣头条实时平台的建设,整理者叶里君.文章将从平台的架构.Flink 现状,Flink 应用以及未来计划四部分分享. 一.平台架构 1.Flink 应用时间线 首先是 ...
- SQL SERVER内部函数大全
SQL SERVER内部函数是SQL数据库中非常重要的一类函数,下面就为您介绍SQL SERVER内部函数,如果您对此方面感兴趣的话,不妨一看. SQL SERVER内部函数: select @@CO ...
- 「树的直径」BFS方法证明
选定任意一个点u,从u开始BFS求出距离u最大的点s,再从s点出发BFS到距离s最大的点t,则dis(s,t)即为树的直径 证明 其实只要找到了树的直径的一个端点,再BFS找到最远点就一定是直径的另一 ...
- 在使用KVO遇到的一个问题
在项目开发中定义了一个单例对象RHUserData的对象,RHOLUserInfo类是单例对象的一个property属性,RHOLUserInfo里面有个userId的属性,在其他类里面进行设置KVO ...
- socket | netcat 模拟
#!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...
- git filter-branch
https://github.com/git-for-windows/git/issues/2206 https://git-scm.com/docs/git-filter-branch The -- ...
- PHP+JS的信息提示弹窗
基于PHP函数的Msg信息提示框 1.可以设置弹出信息,跳转地址,跳转的时间,跳转的信息标题提示: 2.代码实例: <?php function ShowMsg($msg, $gourl,$ti ...
- Yahoo! 35条网站性能优化建议
Yahoo! 35条网站性能优化建议 Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客 ...
- vim如何达到高效
参考:http://blog.jobbole.com/44891/ 搜索技巧 1. 使用*快速查询当前光标所在的单词 然后使用n快速找到下一个查询结果: 使用N快速找到上一个查询结果 2. 在.vim ...