题目链接

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的更多相关文章

  1. LC 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 解题报告(C++ & python)

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

  4. 【leetcode】990. Satisfiability of Equality Equations

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

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

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

  6. 【medium】990. 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. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  9. [leetcode] 399. Evaluate Division

    我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...

随机推荐

  1. Halcon中循环的相关算子

    条件<condition> ,<condition> 内为计算成an integer or boolean value的表达式. 表达式的值1则条件为真,否则为假. 1.if( ...

  2. node.js主从分布式爬虫

    前言 前文介绍过用Python写爬虫,但是当任务多的时候就比较慢, 这是由于Python自带的http库urllib2发起的http请求是阻塞式的,这意味着如果采用单线程模型,那么整个进程的大部分时间 ...

  3. Protocol buffer的使用案例

    Protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了多种语言的实现:java.c#.c++.go 和 python,每一种实 ...

  4. easyui的tab标签链接aspx页面引发全局刷新的问题解决方案

    通过tree组件和tabs组件结合加载子页面窗体aspx,点击按钮页面全部重新加载,或整个跳到子窗体页面,解决方案:换一种结合iframe的方式做系统界面:在tree组件出替换掉设置href属性处为下 ...

  5. 通过NPM快速发布你的NodeJS模块(组件包)

    1.更新 NPM - [ npm install -g npm | 该步骤可选:最好使用新版本] 楼主当前版本号 2.6.1 ,如果更新报错,可以尝试 国内淘宝镜像 $ npm -v 2.6.1 // ...

  6. No.110_第三次团队会议

    前端的易帜 前端在整个软件中有着举足轻重的地位.前端设计一般可以理解为视觉设计,前端开发则是前台代码的实现. 随着科技水平的提高和生产力的提高,人民对于审美的要求逐渐增高.在没有科技壁垒的情况下,是否 ...

  7. 20135234mqy

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java实验    班级:1352    姓名: mqy  学号:20135234 成绩:             指导教师 ...

  8. 【每日scrum】第一次冲刺day2

    和小伙伴一起找地图 ,学习了mapinfo地图格式的基本知识,数据和图像分开存储

  9. C# Linq找不到行或已更改

    前段时间工作中的一个新需求,有机会用到了Linq to SQL.使用后的第一感觉,就是方便很多,也为整个项目节约了一大把的开发时间,甚至代码量也少了很多.不过在程序的实际运行中,始终会遇到一些莫名其妙 ...

  10. install4j 工具为java程序打包exe

    用 install4j 工具为java程序打包exe 制作人:mark 制作时间:2013-05-02 用Eclipse 将程序源码打包成jar文件. 打包jar方法我不做介绍了,相信大家都会,不会的 ...