Redundant Connection——LeetCode进阶路
原题链接https://leetcode.com/problems/redundant-connection/
题目描述
In this problem, a tree is an undirected graph that is connected and has no cycles.
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
1
/
2 - 3
Example 2:
Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
| |
4 - 3
Note:
The size of the input 2D-array will be between 3 and 1000.
Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.
思路分析
题目要求很简单,就是要把给出的无向图的最后一条边删掉
和之前的解法类似,对遍历过的元素标记,发现环时,删掉最后元素
AC解
class Solution {
public int[] findRedundantConnection(int[][] edges) {
if(edges == null || edges.length == 0 || edges[0].length == 0)
{
return new int[2];
}
ArrayList<Integer>[] list = new ArrayList[edges.length + 1];
for(int i=0; i<list.length; i++)
{
list[i] = new ArrayList<Integer>();
}
for(int i=0; i<edges.length; i++)
{
int u = edges[i][0];
int v = edges[i][1];
list[u].add(v);
list[v].add(u);
boolean[] flag = new boolean[edges.length + 1];
if(!dfs(u,list,-1,flag))
{
return edges[i];
}
}
return new int[2];
}
public boolean dfs(int u, ArrayList<Integer> list[],int last, boolean[] flag)
{
if(flag[u])
{
return false;
}
flag[u] = true;
for(int i:list[u])
{
if(i != last)
{
if(!dfs(i,list,u,flag))
{
return false;
}
}
}
return true;
}
}
Redundant Connection——LeetCode进阶路的更多相关文章
- [LeetCode] Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [LeetCode] 685. Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] 684. Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- LeetCode 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- [LeetCode] 685. Redundant Connection II 冗余的连接之 II
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- LN : leetcode 684 Redundant Connection
lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...
- [Swift]LeetCode684. 冗余连接 | Redundant Connection
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- leetcode 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...
随机推荐
- Spark - spark on yarn 的作业提交流程
YarnClient YarnCluster 客户端(Client)通过YARN的ResourceManager提交应用程序.在此过程中,客户端进行权限验证,生成Job ID和资源上传路径,并将这些信 ...
- Scala查看源码
package com.wyh.day01 /** * 1.代码格式化的快捷键 ctrl+alt+L\ * 2.scala查看源代码的快捷键 ctrl+b */ object ScalaLookSou ...
- P11620 [Ynoi Easy Round 2025] TEST_34
由子序列和最值异或可以想到线性基 发现其实线性基满足结合律 考虑线段树进行维护 那么显然的一个想法就是把1操作直接上tag 但是发现上tag其实会丢失线性基的性质 于是差分 将区间修改变为单点修改 考 ...
- AI时代的灵魂拷问:我们真正的核心竞争力到底是什么?
"当所有人都在谈论AI+的时候,今天我想聊一点不一样的..." 上周,朋友看着我用Cursor在30分钟内完成了他过去需要两天才能完成的工作. 那一刻,一种强烈的危机感涌上心头,他 ...
- xss学习及xss-lab解题记录
什么是XSS(跨站脚本攻击) SQL注入是服务端将用户输入的数据当成SQL代码去执行 XSS可以理解为服务端把用户输入的数据当成前端代码去执行 前端代码->主要是js代码 两个关键条件: 第一个 ...
- halo配置踩坑过程小记
写在最前: 终于搞定了最后的一步域名解析配置,其实动态博客的折腾程度也不低于当时的hexo吧,也可能当时的痛苦过程已经忘了..整理一下思路,记录一下配置过程走过的坑. 我是从hexo用了半年想 ...
- linux防火墙查看状态firewall
一.firewall防火墙 1.查看firewall服务状态 systemctl status firewalld 出现Active: active (running)切高亮显示则表示是启动状态. 出 ...
- 如何在linux中查看cpu信息、机器硬件型号
# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz (看到有8 ...
- ANSYS 导出节点的位移数据
1. 数据保存 确定待提取的节点编号: 获取节点位移变量: 将节点位移变量存储到数组中,用于数据传递: ! 输出对应节点的位移到csv文件 ! 注意同时导入.db和.rst,并切换到/post26模块 ...
- 支持VS2022的包发布工具NuPack 2022发布
我们很高兴地宣布 NuPack 2022 正式发布!这是一个开源项目,旨在简化 .NET 开发中的 NuGet 包发布流程. NuPack 是什么? NuPack 是一个轻量级工具,VS扩展,它可以帮 ...