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 <= 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 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 ...
随机推荐
- Go语言的反射
反射是语言里面是非常重要的一个特性,我们经常会看见这个词,但是对于反射没有一个很好的理解,主要是因为对于反射的使用场景不太熟悉. 一.理解变量的内在机制 1.类型信息,元信息,是预先定义好的,静态的. ...
- java项目中注解使用——整理
文章:@Mapper注解的使用 地址:https://blog.csdn.net/weixin_39666581/article/details/81057385 @Mapper注解的的作用 1:为了 ...
- Codeforces #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- python_面向对象——编程步骤
校园管理系统: 设计一个学校机构管理系统,有总部.分校.有学院.老师.员工,实现具体如下需求: 1.有多个课程,课程要有定价 2.有多个班级,班级跟课程有关联 3.有多个学生,学生报名班级,交这个班级 ...
- 阿里云轻量级服务器和NGINX部署Django项目
部署条件: 1.一台阿里云服务器(本人的是CentOS系统的服务器) 2.已经构建好的项目 3.服务器上安装并配置Nginx 首先第一步:在服务器上安装并配置Nginx 进入服务器 $ ssh roo ...
- 2019牛客暑期多校训练营(第九场)The power of Fibonacci——循环节&&CRT
题意 求 $\displaystyle \sum_{i=1}^n F_i^m $,($1 \leq n\leq 10^9,1 \leq m\leq 10^3$),答案对 $10^9$ 取模. 分析 ...
- Nginx 负载均衡条件下 Tomcat 共享Session (Java)(一)
1.修改tomcat 下 conf/context.xml 在</Context>里面加入以下代码 <Valve className="com.orangefunctio ...
- / WebAPP开发与小程序 / 步骤一 · 4-5 地图搜索与poi结合(2)
/ WebAPP开发与小程序 / 步骤一 · 4-5 地图搜索与poi结合(2) 在地图中搜索指定对象时,搜索结果可以显示出每个对象的图片,就差这个不会了
- GreenPlum/postgres copy命令导出/导入数据
一.COPY命令简单实用 1.copy在postgres与GreenPlum介绍 1.1 postgrespostgres的COPY命令可以快速的导出/导入数据到postgresql数据库中,支持常用 ...
- 【HTTP】协议详解
什么是HTTP协议 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端 ...