From f53e5114d60f58b0de1ef476cd6f89eeef5056fc Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 8 Aug 2019 21:59:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20990?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0. Satisfiability of Equality Equations.go | 22 +++++++ ...tisfiability of Equality Equations_test.go | 62 ++++++++++++++++++ .../README.md | 65 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations.go create mode 100644 Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations_test.go create mode 100755 Algorithms/0990. Satisfiability of Equality Equations/README.md diff --git a/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations.go b/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations.go new file mode 100644 index 00000000..0ab59367 --- /dev/null +++ b/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations.go @@ -0,0 +1,22 @@ +package leetcode + +func equationsPossible(equations []string) bool { + if len(equations) == 0 { + return false + } + uf := UnionFind{} + uf.init(26) + for _, equ := range equations { + if equ[1] == '=' && equ[2] == '=' { + uf.union(int(equ[0]-'a'), int(equ[3]-'a')) + } + } + for _, equ := range equations { + if equ[1] == '!' && equ[2] == '=' { + if uf.find(int(equ[0]-'a')) == uf.find(int(equ[3]-'a')) { + return false + } + } + } + return true +} diff --git a/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations_test.go b/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations_test.go new file mode 100644 index 00000000..75b10c33 --- /dev/null +++ b/Algorithms/0990. Satisfiability of Equality Equations/990. Satisfiability of Equality Equations_test.go @@ -0,0 +1,62 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question990 struct { + para990 + ans990 +} + +// para 是参数 +// one 代表第一个参数 +type para990 struct { + a []string +} + +// ans 是答案 +// one 代表第一个答案 +type ans990 struct { + one bool +} + +func Test_Problem990(t *testing.T) { + + qs := []question990{ + + question990{ + para990{[]string{"a==b", "b!=a"}}, + ans990{false}, + }, + + question990{ + para990{[]string{"b==a", "a==b"}}, + ans990{true}, + }, + + question990{ + para990{[]string{"a==b", "b==c", "a==c"}}, + ans990{true}, + }, + + question990{ + para990{[]string{"a==b", "b!=c", "c==a"}}, + ans990{false}, + }, + + question990{ + para990{[]string{"c==c", "b==d", "x!=z"}}, + ans990{true}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 990------------------------\n") + + for _, q := range qs { + _, p := q.ans990, q.para990 + fmt.Printf("【input】:%v 【output】:%v\n", p, equationsPossible(p.a)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0990. Satisfiability of Equality Equations/README.md b/Algorithms/0990. Satisfiability of Equality Equations/README.md new file mode 100755 index 00000000..2d32532b --- /dev/null +++ b/Algorithms/0990. Satisfiability of Equality Equations/README.md @@ -0,0 +1,65 @@ +# [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. `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 `'='` + + + +## 题目大意 + +给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。  + +提示: + +1. 1 <= equations.length <= 500 +2. equations[i].length == 4 +3. equations[i][0] 和 equations[i][3] 是小写字母 +4. equations[i][1] 要么是 '=',要么是 '!' +5. equations[i][2] 是 '=' + + + +## 解题思路 + + +- 给出一个字符串数组,数组里面给出的是一些字母的关系,只有 `'=='` 和 `'! ='` 两种关系。问给出的这些关系中是否存在悖论? +- 这一题是简单的并查集的问题。先将所有 `'=='` 关系的字母 `union()` 起来,然后再一一查看 `'! ='` 关系中是否有 `'=='` 关系的组合,如果有,就返回 `false`,如果遍历完都没有找到,则返回 `true`。