LC 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 <= 500equations[i].length == 4equations[i][0]andequations[i][3]are lowercase lettersequations[i][1]is either'='or'!'equations[i][2]is'='
class Solution {
private:
int arr[];
public:
void unionab(int a, int b) {
arr[parent(a)] = arr[parent(b)];
}
int parent(int a) {
if(arr[a] != a) return parent(arr[a]);
return a;
}
bool uninit(int a) {
return arr[a] == a ? true : false;
}
bool hassameroot(int a, int b) {
return parent(a) == parent(b);
}
bool equationsPossible(vector<string>& equations) {
for(int i=; i<; i++) arr[i] = i;
for(int i=; i<equations.size(); i++) {
int a = ((int)equations[i][] - (int)'a');
int b = ((int)equations[i][] - (int)'a');
if ((int)equations[i][] == (int)'=') {
if(!hassameroot(a,b)) unionab(a,b);
}
}
for(int i=; i<equations.size(); i++) {
int a = ((int)equations[i][] - (int)'a');
int b = ((int)equations[i][] - (int)'a');
if((int)equations[i][] == (int)'!') {
if(hassameroot(a,b)) return false;
}
}
return true;
}
};
LC 990. Satisfiability of Equality Equations的更多相关文章
- 【medium】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 ...
- 【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 ...
- 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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- java - day019 - 数据库
https://www.cnblogs.com/myxq666/p/7787744.html Mac 安装MySQL步骤 什么是数据库 数据库: 英文名称Database ,简称 DB 数据库是按照数 ...
- java - day011 - 集合, ArrayList HashMap,HashSet, Iterator 接口, for-each 循环格式
集合 ArrayList 丑数: 能被3,5,7整除多次, ArrayList list 接口 | - ArrayList | - Linked ...
- 剖析可执行文件ELF组成
对比参考:剖析.o文件ELF组成 相比.o的ELF格式,有哪些变化? .rel.text和.rel.data消失了 为什么这两个节会消失? 链接器将各.o中同名的.text和.data节整合到一起时, ...
- sql基本操作之增删改查
1. 显示数据库 show databases; show databases; 2. 显示当前数据库 select current_database(); 3. 创建/删除数据库 create da ...
- 基于Java+Selenium的WebUI自动化测试框架(二)-----页面操作接口
在有了基础的Position类之后,我们需要考虑我们在寻找完页面元素之后,需要做什么.这个“做”什么,可以理解为我们在页面上需要对应的一系列动作.比如:点击,输入,切换窗口,寻找元素,判断元素是否存在 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...
- k8s的pod的资源调度
1.常用的预选策略 2.优选函数 3.节点亲和调度 3.1.节点硬亲和性 3.2.节点软亲和性 4.Pod资源亲和调度 4.1.Pod硬亲和度 4.2.Pod软亲和度 4.3.Pod反亲和度 5.污点 ...
- redis 事务 & 锁
参考:https://www.cnblogs.com/DeepInThought/p/10720132.html Redis不保证原子性:Redis中,单条命令是原子性执行的,但事务不保证原子性,且没 ...
- GITHUB下载源码方式
从昨天开始就想着从GitHub上下载一个开源的Vue的实战项目,希望能从中学习更多的Vue的实用内容,结果搞了半天好不容易下载了,不知道怎么弄.然而,今天终于成功了,激动地我赶紧来记录一下.如何从Gi ...
- ipv4枯竭和ipv6的启用
IPv4是Internet Protocol version 4的缩写,中文翻译为互联网通信协议(TCP/IP协议)第四版,通常简称为网际协议版本4. IPv4使用32位(4字节)地址,因此地址空间中 ...