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 ...
随机推荐
- SpringMVC - 谈谈你对SpringMVC的理解
谈谈你对 Spring MVC 的理解? 普通人:Spring MVC 它是一个MVC框架吧,就是,我们可以使用Spring MVC来开发Web应用...呃 它是基于Servlet上的一个扩展,就是它 ...
- CF2018C Tree Pruning
分析 好像官方题解是反向求解的,这里提供一个正向求解的思路,即直接求出最后所有叶节点到根的距离相同为 \(x\) 时需要删除的结点数 \(ans_x\) . 如果我们最后到根的相同距离为 \(x\), ...
- Zookeeper Java客户端连接慢、超时问题Ad-Hoc检查清单
TL;DR 排查思路: 首先确认你的设备到zookeeper的连通性是OK的,可通过命令echo srvr | nc HOST 2181,检查是否可以正常打印节点信息.windows用户可以在命令行输 ...
- springboot报错说 Failed to parse multipart servlet request; nested exception is java.io.IOException
问题:一次开发中遇到一个springboot的异常,如下所示:Failed to parse multipart servlet request; nested exception is java.i ...
- css 技巧:利用 after 伪对象和 background 属性实现 img 图片标签占位图
同步发布:https://blog.jijian.link/2020-04-15/css-img-after-placeholder/ 如图: 图片加载失败了,在浏览器会默认显示一张破图.受各种网速. ...
- react使用插件配置px转换为vw
react配置px转换为vw 1.下载postcss-px-to-viewport插件 npm install postcss-px-to-viewport --save-dev 2.下载craco ...
- Kubernetes的工作机制
云计算时代的操作系统 Kubernetes 是一个生产级别的容器编排平台和集群管理系统,能够创建.调度容器,监控.管理服务器. Kubernetes 的基本架构 操作系统的一个重要功能就是抽象,从繁琐 ...
- deepseek内网离线部署手册
前言 在当下 AI 浪潮汹涌的时代,DeepSeek 以其卓越的性能和出色的表现,迅速成为了众多专业人士和科技爱好者热议的焦点工具.在众多AI大模型的比拼中,DeepSeek 展现出了优越的实力.然而 ...
- burp suite使用(一) --- 抓包,截包,改包
接下来我将以一个新手的角度讲述如何使用burp来抓包,截包和改包. 我采用的是UC浏览器来配合burp的使用. 1.设置浏览器 设置---其他---更改代理设置 由于UC浏览器采用的是ie的内核,所以 ...
- Delphi CheckListBox 用法
for i := CheckListBox1.Items.Count-1 downto 0 do //从后面往前面删 begin if CheckListBox1.Checked[i] then // ...