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 ...
随机推荐
- C++——virtual function
无论是pure virtual还是impure virtual,都允许子类override他.但是真两种方式还是有一点差别,如果是pure virtual,那么父类是十分强烈希望子类override他 ...
- Python_关键字列表
1.Python关键字列表
- 【转】provisional headers are shown 知多少
前言 请求里面provisional headers are shown(显示临时报头) 出现的情况很多,但原因是多样的.如果你去直接匹配关键字搜索,得到的结果可能与你自己的情况大相径庭.网上大部分都 ...
- P2P system: FastTrack and BitTorrent
FastTrack FastTrack来源于Gnutella,是Gnutella 和 Napster的杂交体 有些node承担了更重要的责任,这些nodes称为supernodes,因为这些改进,它比 ...
- pandas模块的基本用法
一.读取文件 import pandas as pd data = pd.read_csv("F:\\ml\\机器学习\\01\\score.csv") #一般读取的是csv文件, ...
- selenium.webdriver获取结果转为json格式
from selenium import webdriver driver.get(requestUrl)html = driver.page_sourcesoup = BeautifulSoup(h ...
- UWB DWM1000 开源项目框架
UWB 目前比较火热,不论国内还是国外目前都掀起一股热潮. 但是实际工程代码很少,开源代码更少. 目前代码主要有 1 DecaWave Release的定位源码,代码基于TWR,一个非常大的状态机. ...
- reCAPTCHA打不开的解决方法
reCAPTCHA打不开的解决方法 by WernerPosted on2018年1月8日 reCAPTCHA是国外广泛使用的验证码,但由于一些原因国内无法使用. 观察使用reCAPTCHA的网站,发 ...
- try except else finally
try..except..else没有捕获到异常,执行else语句 try..except..finally不管是否捕获到异常,都执行finally语句
- Django基础之创建admin账号
1. 首先我们要新建一个用户名, 用来登录管理网站,可以使用如下命令: python manage.py createsuperuser 2. 输入想要使用的用户名: Username(leave b ...