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个点是否 ...
随机推荐
- Java学习之旅(二):生病的狗1(逻辑推导)
前言:本篇文章属于个人笔记,例化了一些代码,不知是否合理,请指教. 中午看到一位同学的面试题,觉得很烧脑,烧脑不能一个人烧,要大家一起烧. 村子中有50个人,每人有一条狗.在这50条狗中有病狗(这种病 ...
- C语言环境搭建
UNIX/Linux 上的安装 如果您使用的是 Linux 或 UNIX,请在命令行使用下面的命令来检查您的系统上是否安装了 GCC: $ gcc -v 如果您的计算机上已经安装了 GNU 编译器,则 ...
- Source Insight 4.0相对路径的设置
比如在D盘有个51的Firmware工程 里面有几个文件夹存放程序文件,项目文件在Project文件夹里,也就是整个Firmware里的文件都是有用的. Keil项目文件位置. 到这里就可以双击Tem ...
- Golang转义字符
Golang常见的转义字符(escape char) \t 一个制表位,实现对齐的功能 \n 换行符 \\ 一个\ \" 一个" \r 一个回车 fm ...
- EF DBFirst 实体生成命令
dbfirst Entity生成命令行: Scaffold-DbContext "PORT=5435;DATABASE=DocumentDB;HOST=localhost;PASSWORD= ...
- Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口
WebClient是Spring提供的非阻塞.响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate.文中所使用到的软件版本:Java 1 ...
- 协程和Goroutines示例
一. 协程的定义 Coroutines are computer-program components that generalize subroutines for non-preemptive m ...
- vue自定义组件的递归
1.设置一个div显示数值(这个数值不要设置在组件内部) 2.设置组件的名字 <!DOCTYPE html> <html> <head> <title> ...
- Linux添加硬盘创建新的逻辑卷方式
有同仁看了上文<Linux添加硬盘扩充已有分区存储空间方式>一文后,提出疑问,现在很多云服务器本来没有逻辑卷,添加数据盘后需要自行添加,如何处理? 此文将以某云服务器为例,详细进行解说. ...
- 【书评:Oracle查询优化改写】第三章
[书评:Oracle查询优化改写]第三章 BLOG文档结构图 导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① 隐含参数 ...