Satisfiability of Equality Equations - LeetCode
题目链接
Satisfiability of Equality Equations - LeetCode
注意点
- 必须要初始化pre
解法
解法一:典型的并查集算法应用。先遍历所有等式,将等号两边的字母加入同一分类,每类中的字母都是相等的。然后遍历不等式,如果不等号两边的字母属于同一类则返回false。时间复杂度O(nm)
class Solution {
public:
map<char,char> pre;
char Find(char x)
{
char r = x;
while(pre[r] != r)
{
r = pre[r];
}
return r;
}
void Join(char x,char y)
{
char fx = Find(x);
char fy = Find(y);
if(fx != fy) pre[fx] = fy;
}
void Init()
{
char letter[26] ={'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z'};
for (auto l:letter)
{
pre[l] = l;
}
}
bool equationsPossible(vector<string>& equations) {
Init();
for(auto e:equations)
{
if(e[1] == '=') Join(e[0],e[3]);
}
for(auto e:equations)
{
if(e[1] == '!' && Find(e[0]) == Find(e[3]))
{
return false;
}
}
return true;
}
};

小结
Satisfiability of Equality Equations - LeetCode的更多相关文章
- 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 ...
- 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...
- 【leetcode】990. Satisfiability of Equality Equations
题目如下: Given an array equations of strings that represent relationships between variables, each strin ...
- [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations
Given an array equations of strings that represent relationships between variables, each string equa ...
- 【medium】990. 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 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
随机推荐
- SSM搭项目报错:HTTP Status 400 – Bad Request
具体报错如下: Type Status Report Description The server cannot or will not process the request due to some ...
- Echarts服务端生成图片
Echarts是百度发布的一套优秀的浏览器端图表控件,Echarts是基于html5的cavens绘图实现.而使用server端生成图片无法借用浏览器端渲染.通用的做法有两种: 是用headless浏 ...
- C#中字符串 "驻留"与Lock(转载)
class TestWorker 2 { 3 public void DoMultiThreadedWork(object someParameter) 4 { 5 ...
- SQL IF while 游标
-- if语句使用示例 declare @a int set @a=1 begin print @a =@a+1 end else begin print 'noooo' end -- while语句 ...
- python操作hive并且获取查询结果scheam
执行hive -e 命令并且获取对应的select查询出来的值及其对应的scheam字段 需要在执行语句中前部添加 set hive.cli.print.header=true; 这个设置,如下语句: ...
- psp报告
1.读材料回答问题 (1)回想一下你曾经对计算机专业的畅想.当初你是如何做出选择计算机专业的决定的?你认为过去接触到的课程是否符合你对计算机专业的期待,为什么?你觉得计算机是你喜欢的领域吗,它是你擅长 ...
- 20180711-统计PDB中的蛋白质种类、膜蛋白文件个数及信息等
20180710完成这份工作.简单,但是完成了还是很开心.在我尝试如何使用pickle保存数据后,尝试保存PDB文件中“HEADER”中的信息.文件均保存于实验室服务器(97.73.198.168)/ ...
- 查询数据库时mapper报错:It's likely that neither a Result Type nor a Result Map was specified.
因为mapper.xml里把resultType写成了parameterType
- Chapter 5 软件工程中的形式化方法
从广义上讲,形式化方法是指将离散数学的方法用于解决软件工程领域的问题,主要包括建立精确的数学模型以及对模型的分析活动.狭义的讲,形式化方法是运用形式化语言,进行形式化的规格描述.模型推理和验证的方法. ...
- ASP.NET中实现封装与策略模式
首先把运算方法封装起来,这样在网页界面中直接就可以调用了,不过是换张脸而已! using System; using System.Collections.Generic; using System. ...