LeetCode 990. Satisfiability of Equality Equations
原题链接在这里:https://leetcode.com/problems/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 <= 500equations[i].length == 4equations[i][0]andequations[i][3]are lowercase lettersequations[i][1]is either'='or'!'equations[i][2]is'='
题解:
First, go through equations, union all with "==".
Then, go through equations again, if a!=b, but a and b are in the same union, then return false.
Time Complexity: O(nlogk). n = equations.length. k is number distinct character. Since k is single lower case, thus k <= 26.
Space: O(k).
AC Java:
class Solution {
HashMap<Character, Character> parent;
public boolean equationsPossible(String[] equations) {
if(equations == null || equations.length == 0){
return true;
}
parent = new HashMap<>();
for(String s : equations){
if(s.substring(1,3).equals("==")){
union(s.charAt(0), s.charAt(3));
}
}
for(String s : equations){
if(s.substring(1,3).equals("!=") && find(s.charAt(0))==find(s.charAt(3))){
return false;
}
}
return true;
}
private void union(char i, char j){
char p = find(i);
char q = find(j);
if(p != q){
parent.put(p, q);
}
}
private char find(char c){
parent.putIfAbsent(c, c);
if(parent.get(c) != c){
char ancestor = find(parent.get(c));
parent.put(c, ancestor);
}
return parent.get(c);
}
}
LeetCode 990. Satisfiability of Equality Equations的更多相关文章
- 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 解题报告(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 ...
- 【medium】990. Satisfiability of Equality Equations 并查集
Given an array equations of strings that represent relationships between variables, each string equa ...
- 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 说明:题目中含有$符号则为付费题目. 如 ...
- [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个点是否 ...
随机推荐
- python+selenium实现自动化百度搜索关键词
通过python配合爬虫接口利用selenium实现自动化打开chrome浏览器,进行百度关键词搜索. 1.安装python3,访问官网选择对应的版本安装即可,最新版为3.7. 2.安装seleniu ...
- 20.Python略有小成(面向对象Ⅱ)
Python(面向对象Ⅱ) 一.类的空间问题 何处可以添加对象属性 class A: def __init__(self,name): self.name = name def func(self,s ...
- epoll原理
系统调用说明 epoll_create:在内核中创建epoll结构 epoll_ctl:add 1. 调用监听的文件的poll方法,设置callback 2. 设备就绪时唤醒等待队列上的进程,此时会调 ...
- oracle学习笔记(三)
索引: drop table test1 purge; drop table test2 purge; drop table test3 purge; drop table t purge; crea ...
- ExtractFileDir 与 ExtractFilePath 的区别
ExtractFileDir 从文件名中获取目录名(文件不在根目录下时取得的值后没有“/”,在根目录时一样,都是盘符,例如“C:/”) ExtractFilePath 从文件名中获取路径名(文件不在根 ...
- mysql 8.0.17 安装与使用
目录 写在前面 MySQL 安装 重置密码 使用图形界面软件 Navicat for SQL 写在前面 以前包括现在接到的项目,用的最多的关系型数据库就是SqlServer或者Oracle.后来因为接 ...
- C#录制声卡声音喇叭声音音箱声音
在项目中,我们会需要录制电脑播放的声音,比如歌曲,电影声音,聊天声音等通过声卡音箱发出的声音.那么如何采集呢?当然是采用SharpCapture!下面开始演示关键代码,您也可以在文末下载全部源码: 设 ...
- The underlying connection was closed: An unexpected error occurred on a rece
服务器问题,在后台访问外网了,特别是https的网站,容易出这个问题. 修改服务器配置,或修改代码解决.
- CentOS -- 新建用户并使能密钥登录
目录 1. 新建用户 2. 为新用户授权 2.1. 方法一:把新用户添加到wheel用户组中 2.2. 方法二:把新用户添加到sudoers列表中 3. 新用户使能 SSH 密钥登录 4. 其它 4. ...
- JVM性能优化--字节码技术
一.字节码技术应用场景 AOP技术.Lombok去除重复代码插件.动态修改class文件等 二.字节技术优势 Java字节码增强指的是在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于 ...