leetcode每日一题(2020-06-08):990. 等式方程的可满足性

题目描述:
给定一个由表示变量之间关系的字符串方程组成的数组,
每个字符串方程 equations[i] 的长度为 4,
并采用两种不同的形式之一:"a==b" 或 "a!=b"。
在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。
只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。

今日学习:
1.forEach()也适用于Map,但是要记着forEach(func(value, index, arr), thisValue)中参数的顺序
2.HashMap中的get,set,forEach等各种用法:1&2
3.并查集
题解1:我自己想出来的,比较啰嗦

var equationsPossible = function(equations) {
    //我居然能做出来,虽然时间很慢很慢哈哈哈哈哈哈哈哈哈哈哈哈哈
    let code = new Map()
    for(let i = 0; i < equations.length; i++) {
        if(equations[i][1] == '=' && equations[i][2] == '=') {
            if(!code.get(equations[i][0]) && !code.get(equations[i][3])){
                code.set(equations[i][0], equations[i][0])
                code.set(equations[i][3], equations[i][0])
            }else if(code.get(equations[i][0]) && !code.get(equations[i][3])){
                code.set(equations[i][3], code.get(equations[i][0]))
            }else if(!code.get(equations[i][0]) && code.get(equations[i][3])){
                code.set(equations[i][0], code.get(equations[i][3]))
            }else if(code.get(equations[i][0]) != code.get(equations[i][3])){
                temp = code.get(equations[i][0])
                change = code.get(equations[i][3])
                code.forEach((value, key)=>{
                    if(value == temp){
                        code.set(key, change)
                    }
                })
            }
        }
    }
    code.entries
    for(let i = 0; i < equations.length; i++) {
        if(equations[i][1] == '!') {
            if(code.get(equations[i][0]) && code.get(equations[i][3])) {
                if(code.get(equations[i][0]) == code.get(equations[i][3])) {
                    return false
                }
            }else if(equations[i][0] == equations[i][3]) {
                return false
            }
        }
    }
    return true
};

题解2:天使大佬的并查集

var equationsPossible = (equations) => {
  const uf = new UnionFind(26)
  for (const e of equations) { // 将字母对应成数字
    if (e[1] === '=') uf.union(e.charCodeAt(0) - 97, e.charCodeAt(3) - 97) 
  }
  for (const e of equations) {
    if (e[1]=='!'&&uf.findRoot(e.charCodeAt(0)-97)==uf.findRoot(e.charCodeAt(3)-97))
      return false
  }
  return true
}

class UnionFind {
  constructor(num) { // num 顶点个数
    this.roots = new Array(num)
    this.ranks = new Array(num)
    for (let i = 0; i < num; i++) {
      this.roots[i] = -1
      this.ranks[i] = 0
    }
  }
  findRoot(x) { // 找出顶点x的根节点
    let x_root = x
    while (this.roots[x_root] !== -1) { // 一直找父节点,找到尽头
      x_root = this.roots[x_root]
    }
    return x_root // 返回根节点
  }
  union(x, y) { // 把顶点x和顶点y合并到一起
    let x_root = this.findRoot(x)
    let y_root = this.findRoot(y)
    if (x_root === y_root) return
    let x_rank = this.ranks[x_root]
    let y_rank = this.ranks[y_root]
    if (x_rank < y_rank) {    // 谁高度大,谁就作为root
      this.roots[x_root] = y_root
    } else if (y_rank < x_rank) {
      this.roots[y_root] = x_root
    } else {                  // 一样深,谁作为root都行
      this.roots[y_root] = x_root
      this.ranks[x_root]++
    }
  }
}
原文地址:https://www.cnblogs.com/autumn-starrysky/p/13066264.html