当前位置: 首页 > news >正文

LeetCode每日一题(990. Satisfiability of Equality Equations)

You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: “xi==yi” or “xi!=yi”.Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.

Example 1:

Input: equations = [“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: equations = [“ba","ab”]
Output: true

Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Constraints:

  • 1 <= equations.length <= 500
  • equations[i].length == 4
  • equations[i][0] is a lowercase letter.
  • equations[i][1] is either ‘=’ or ‘!’.
  • equations[i][2] is ‘=’.
  • equations[i][3] is a lowercase letter.

就是个 graph 的问题, 如果节点 a 与节点 b 是联通的(a == b), 同时又要求 a 与 b 不能联通(a != b), 则返回 false, 其他情况返回 true。 具体的办法还是用 union find 的方法, 我们只对==做 union, 然后用!=来做检查, 只要没有两个不等式左右两边的节点具有同一个 parent, 则返回 true



impl Solution {
    fn find(idx: usize, parents: &mut Vec<usize>) -> usize {
        if parents[idx] == idx {
            return idx;
        }
        let parent = Solution::find(parents[idx], parents);
        parents[idx] = parent;
        parent
    }

    fn union(a: usize, b: usize, parents: &mut Vec<usize>) {
        let a_parent = Solution::find(a, parents);
        let b_parent = Solution::find(b, parents);
        parents[b_parent] = a_parent;
    }
    pub fn equations_possible(equations: Vec<String>) -> bool {
        let mut parents: Vec<usize> = (0..26).into_iter().collect();
        let mut not_equals = Vec::new();
        for equation in equations {
            let components: Vec<char> = equation.chars().collect();
            if components[1] == '=' {
                Solution::union(
                    components[0] as usize - 97,
                    components[3] as usize - 97,
                    &mut parents,
                );
                continue;
            }
            not_equals.push((components[0] as usize - 97, components[3] as usize - 97));
        }
        for (a, b) in not_equals {
            let a_parent = Solution::find(a, &mut parents);
            let b_parent = Solution::find(b, &mut parents);
            if a_parent == b_parent {
                return false;
            }
        }
        true
    }
}

相关文章:

  • 关键字和内置函数
  • 2022-2028全球园艺设备行业调研及趋势分析报告
  • 【Android入门】7、多媒体:用 NotificationChannel 和 NotificationManager 实现系统通知、播放音频和视频
  • eNSP抓包看PPP协议
  • 继SpringCloudAlibaba后阿里又一神作:MySQL应用实战与性能调优
  • 数据库系统概论笔记
  • 读书笔记:《量化投资实务》
  • Dart external关键字
  • 低碳生活进行时!国产“芯”RK3568创造智慧出行新体验
  • 雷达与imu初始化:鲁棒且实时的雷达惯性初始化方法
  • 算法与诗数据结构 --- 查找 --- 线性表的查找
  • UML(用例图进阶)
  • Intel汇编-LOOP循环
  • 基于JAVA社交物联网的服务搜索系统计算机毕业设计源码+数据库+lw文档+系统+部署
  • 【程序员面试金典】17.04. 消失的数字
  • 《剑指offer》分解让复杂问题更简单
  • egg(89)--egg之redis的发布和订阅
  • ES6系统学习----从Apollo Client看解构赋值
  • Javascript Math对象和Date对象常用方法详解
  • Python利用正则抓取网页内容保存到本地
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 反思总结然后整装待发
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 坑!为什么View.startAnimation不起作用?
  • 前端知识点整理(待续)
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • AI算硅基生命吗,为什么?
  • C# - 为值类型重定义相等性
  • Hibernate主键生成策略及选择
  • #100天计划# 2013年9月29日
  • #include<初见C语言之指针(5)>
  • (003)SlickEdit Unity的补全
  • (06)金属布线——为半导体注入生命的连接
  • (12)Linux 常见的三种进程状态
  • (4.10~4.16)
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (C语言)逆序输出字符串
  • (C语言)球球大作战
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (多级缓存)多级缓存
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (六)c52学习之旅-独立按键
  • (三) diretfbrc详解
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .netcore如何运行环境安装到Linux服务器
  • .NET性能优化(文摘)
  • @Autowired和@Resource装配