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. 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 '='
Runtime: 12 ms, faster than 100.00% of C++ online submissions for Satisfiability of Equality Equations.
Memory Usage: 7.1 MB, less than 100.00% of C++ online submissions for Satisfiability of Equality Equations.
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的更多相关文章

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

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

  2. LeetCode 990. Satisfiability of Equality Equations

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

  3. 【leetcode】990. Satisfiability of Equality Equations

    题目如下: Given an array equations of strings that represent relationships between variables, each strin ...

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

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

  5. Satisfiability of Equality Equations - LeetCode

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

  6. [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations

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

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. RestFramework之解析器

    一.什么是解析器? 对请求的数据进行解析 - 请求体进行解析. 解析器在你不拿请求体数据时 不会调用. 安装与使用: https://www.django-rest-framework.org/ 官方 ...

  2. pytorch版yolov3训练自己数据集

    目录 1. 环境搭建 2. 数据集构建 3. 训练模型 4. 测试模型 5. 评估模型 6. 可视化 7. 高级进阶-网络结构更改 1. 环境搭建 将github库download下来. git cl ...

  3. 基于beautifulSoup进行电影网站排名的获取与格式化输出

    要求 编写代码完成以下任务: ① 将地址"http://www.cbooo.cn/year?year=2019"源代码使用任意方法保存到指定文件中(文件类型不限). ② 使用文件流 ...

  4. RT-Thread--线程间通信

    线程中通信 在裸机编程中,经常会使用全局变量进行功能间的通信,如某些功能可能由于一些操作而改变全局变量的值,另一个功能对此全局变量进行读取,根据读取到的全局变量值执行相应的动作,达到通信协作的目的: ...

  5. P1313 计算系数[二项式定理]

    题目描述 给定一个多项式\((by+ax)^k\),请求出多项式展开后\(x^n \times y^m\)项的系数. 解析 一道水题,二项式定理搞定.注意递推组合数时对其取模. 参考代码 #inclu ...

  6. .NET Core 调试出现“应用程序处于中断模式”

    今天再看博客园的大神博客,按照例子写了一个文件读写的示例(对 .NET Core FileProvider 的应用). 在调试的时候出现了“应用程序处于中断模式”的问题,继续的话程序并没有向下执行,看 ...

  7. Linux下安装nginx实现伪分布

    1.安装 Nginx 的编译环境 gcc yum install gcc-c++ 2.nginx 的 http 模块使用 pcre 解析正则表达式,所以安装 perl 兼容的正则表达式库 yum in ...

  8. Java图形界面

    图形界面 JFrame在swingbao JFrame jframe = new JFrame(); iframe.setVisible(true); //设置窗口显示 jframe.setLocat ...

  9. 几个牛X的js开发技巧

    1. 确保数组值 使用 grid ,需要重新创建原始数据,并且每行的列长度可能不匹配, 为了确保不匹配行之间的长度相等,可以使用Array.fill方法. let array = Array(5).f ...

  10. mybatis-传多个参数

    第一种方案:下标传参 DAO层的函数方法 Public User selectUser(String name, String area); 对应的Mapper.xml <select id=& ...