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:

  1. Input: ["a==b","b!=a"]
  2. Output: false
  3. 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:

  1. Input: ["b==a","a==b"]
  2. Output: true
  3. Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

  1. Input: ["a==b","b==c","a==c"]
  2. Output: true

Example 4:

  1. Input: ["a==b","b!=c","c==a"]
  2. Output: false

Example 5:

  1. Input: ["c==c","b==d","x!=z"]
  2. 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.
  1. class Solution {
  2.  
  3. private:
  4. int arr[];
  5. public:
  6.  
  7. void unionab(int a, int b) {
  8. arr[parent(a)] = arr[parent(b)];
  9. }
  10. int parent(int a) {
  11. if(arr[a] != a) return parent(arr[a]);
  12. return a;
  13. }
  14. bool uninit(int a) {
  15. return arr[a] == a ? true : false;
  16. }
  17. bool hassameroot(int a, int b) {
  18. return parent(a) == parent(b);
  19. }
  20.  
  21. bool equationsPossible(vector<string>& equations) {
  22. for(int i=; i<; i++) arr[i] = i;
  23. for(int i=; i<equations.size(); i++) {
  24. int a = ((int)equations[i][] - (int)'a');
  25. int b = ((int)equations[i][] - (int)'a');
  26. if ((int)equations[i][] == (int)'=') {
  27. if(!hassameroot(a,b)) unionab(a,b);
  28. }
  29. }
  30. for(int i=; i<equations.size(); i++) {
  31. int a = ((int)equations[i][] - (int)'a');
  32. int b = ((int)equations[i][] - (int)'a');
  33. if((int)equations[i][] == (int)'!') {
  34. if(hassameroot(a,b)) return false;
  35. }
  36. }
  37. return true;
  38. }
  39. };

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. C++——virtual function

    无论是pure virtual还是impure virtual,都允许子类override他.但是真两种方式还是有一点差别,如果是pure virtual,那么父类是十分强烈希望子类override他 ...

  2. Python_关键字列表

    1.Python关键字列表

  3. 【转】provisional headers are shown 知多少

    前言 请求里面provisional headers are shown(显示临时报头) 出现的情况很多,但原因是多样的.如果你去直接匹配关键字搜索,得到的结果可能与你自己的情况大相径庭.网上大部分都 ...

  4. P2P system: FastTrack and BitTorrent

    FastTrack FastTrack来源于Gnutella,是Gnutella 和 Napster的杂交体 有些node承担了更重要的责任,这些nodes称为supernodes,因为这些改进,它比 ...

  5. pandas模块的基本用法

    一.读取文件 import pandas as pd data = pd.read_csv("F:\\ml\\机器学习\\01\\score.csv") #一般读取的是csv文件, ...

  6. selenium.webdriver获取结果转为json格式

    from selenium import webdriver driver.get(requestUrl)html = driver.page_sourcesoup = BeautifulSoup(h ...

  7. UWB DWM1000 开源项目框架

    UWB 目前比较火热,不论国内还是国外目前都掀起一股热潮. 但是实际工程代码很少,开源代码更少. 目前代码主要有 1 DecaWave Release的定位源码,代码基于TWR,一个非常大的状态机. ...

  8. reCAPTCHA打不开的解决方法

    reCAPTCHA打不开的解决方法 by WernerPosted on2018年1月8日 reCAPTCHA是国外广泛使用的验证码,但由于一些原因国内无法使用. 观察使用reCAPTCHA的网站,发 ...

  9. try except else finally

    try..except..else没有捕获到异常,执行else语句 try..except..finally不管是否捕获到异常,都执行finally语句

  10. Django基础之创建admin账号

    1. 首先我们要新建一个用户名, 用来登录管理网站,可以使用如下命令: python manage.py createsuperuser 2. 输入想要使用的用户名: Username(leave b ...