1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.LeetCode 990 977 Bytes
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2022-12-05 17:52 +08:00 . 等式方程的可满足性
class Solution {
public:
bool equationsPossible(vector<string>& equations) {
vector<int> ufs(26,-1);
auto RootFind = [&ufs](int x)
{
while(ufs[x] >= 0)
x = ufs[x];
return x;
};
for(auto& str : equations)
{
if(str[1] == '=')
{
int root1 = RootFind(str[0] - 'a');
int root2 = RootFind(str[3] - 'a');
if(root1 != root2)
{
ufs[root1] += ufs[root2];
ufs[root2] = root1;
}
}
}
for(auto& str : equations)
{
if(str[1] == '!')
{
int root1 = RootFind(str[0] - 'a');
int root2 = RootFind(str[3] - 'a');
if(root1 == root2)
return false;
}
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助