1 Star 0 Fork 0

wd6/LeetCode-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
redundant-connection.cpp 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(nlog*n) ~= O(n), n is the length of the positions
// Space: O(n)
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
UnionFind union_find(edges.size() + 1);
for (const auto& edge : edges) {
if (!union_find.union_set(edge[0], edge[1])) {
return edge;
}
}
return {};
}
private:
class UnionFind {
public:
UnionFind(const int n) : set_(n), count_(n) {
iota(set_.begin(), set_.end(), 0);
}
int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
bool union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root == y_root) {
return false;
}
set_[min(x_root, y_root)] = max(x_root, y_root);
--count_;
return true;
}
int length() const {
return count_;
}
private:
vector<int> set_;
int count_;
};
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wd6/LeetCode-1.git
git@gitee.com:wd6/LeetCode-1.git
wd6
LeetCode-1
LeetCode-1
master

搜索帮助