【medium】990. Satisfiability of Equality Equations 并查集
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 b
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: 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 <= 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 {
- private:
- int f[];
- void init(){
- for (int i=;i<;i++)
- f[i] = i;
- }
- int get_f(int x) {
- if (int(f[x]) == int(x)) {
- return x;
- }
- return f[x] = get_f(f[x]); // wrong
- }
- void merge(int a, int b){
- a = get_f(a); // wrong
- b = get_f(b);
- f[a] = b;
- }
- bool is_same_set(int a, int b){
- if (get_f(a) == get_f(b))
- return true;
- return false;
- }
- public:
- bool equationsPossible(vector<string>& equations) {
- init();
- for (string s : equations){ // first: ==
- cout<<s<<endl;
- int a = s[] - 'a'; // wrong
- int b = s[] - 'a';
- if (s[] == '=')
- merge(a, b);
- }
- for (string s : equations){ // second: !=
- cout<<s<<endl;
- int a = s[] - 'a'; // wrong
- int b = s[] - 'a';
- if (s[] == '!')
- if (is_same_set(a, b))
- return false;
- }
- return true;
- }
- };
【medium】990. Satisfiability of Equality Equations 并查集的更多相关文章
- 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
题目如下: Given an array equations of strings that represent relationships between variables, each strin ...
- 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...
- 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 解法 解法一:典型的并查集算法 ...
- [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations
Given an array equations of strings that represent relationships between variables, each string equa ...
- [leetcode] 并查集(Ⅱ)
最长连续序列 题目[128]:链接. 解题思路 节点本身的值作为节点的标号,两节点相邻,即允许合并(x, y)的条件为x == y+1 . 因为数组中可能会出现值为 -1 的节点,因此不能把 root ...
- LeetCode:并查集
并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...
- 【并查集专题】【HDU】
PS:做到第四题才发现 2,3题的路径压缩等于没写 How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- Spring Boot – Jetty配置
前言 默认情况下,Spring Boot会使用内置的tomcat容器去运行应用程序,但偶尔我们也会考虑使用Jetty去替代Tomcat:对于Tomcat和Jetty,Spring Boot分别提供了对 ...
- c++ _pFirstBlock == pHead
今天写程序时碰到了这个异常,导致调试的程序卡死.在网上找了很久答案,都没解决.大致判定是对象被多次析构,但又确认程序逻辑没有问题. 后来参考了 http://www.cnblogs.com/qinta ...
- Java的selenium代码随笔(6)
//获取元素列表public List<WebElement> ListElements(WebElement webElement, By parentBy, By childrenBy ...
- 转:eclipse 设置Java快捷键补全
1.打开Eclipse,点击" Window - Preferences"; 2. 在目录树上选择"Java——Editor——Content Assist", ...
- telnet-server、telnet
1.查询yum仓库中的安装包 [root@localhost /]# yum list |grep telnettelnet.x86_64 1 ...
- 【学亮编程手记】Spring Cloud三大组件Eureka/Feign/Histrix的原理及使用
- vue.js实战——.native修饰符
https://blog.csdn.net/qq_29468573/article/details/80771625 除了用v-on在组件上监听自定义事件外,也可以监听DOM事件,这时可以用.nati ...
- DAY19、日常模块
一.hashlib模块:加密1.基本使用:import hashlibcipher = hashlib.md5('需要加密的数据(二进制形式)'.encode('utf-8'))print(ciphe ...
- servlet(6) 链接数据库
一.servlet链接mysql步骤: 1.注册驱动器:Class.forName("com.mysql.jdbc.Driver"); 加载类并执行下面的静态语句块,将Driver ...
- Python——代码汇总
1.三级菜单 2.Windows启动服务 3.常用的Python实现 4.字典的基本操作