原题链接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进阶路的更多相关文章

  1. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  2. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  3. [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 ...

  4. [LeetCode] 684. Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  5. LeetCode 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  6. [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 ...

  7. Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

    Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...

  8. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  9. [Swift]LeetCode684. 冗余连接 | Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  10. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...

随机推荐

  1. Java01 - Scanner对象

    简介 之前我们学的基本语法中并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入.java.util.Scanner是Java5的新特征,我们可以通过Scanner ...

  2. 面试题53 - II. 0~n-1中缺失的数字

    地址:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/ <?php /** 一个长度为n-1的递增排序数组中的所有数字都是唯一的, ...

  3. 【Matlab】输出变量内容到xls文件

    版本: matlab 2017a 功能:将矩阵输出一个excel文件 代码: function [ statu ] = write2xls( x,filename,sheet) % 返回值: % 1 ...

  4. @SpringBootApplication自动配置原理

    @EnableAutoConfiguration 是核心,他会调用一个@Import注解.我们已知Import自动配置得实现是通过创建ImportSelector 接口的实现类并重写里面selectI ...

  5. k8s v1.19版本之后,自签证书过期x509: certificate has expired or is not yet valid

    前言 在 Kubernetes 1.16 版本之前,kubeadm 工具的 alpha certs 子命令用于生成和管理 Kubernetes 集群的证书.然而,从 Kubernetes 1.19 版 ...

  6. python 更新pip镜像源

    前言 默认情况下 pip 使用的是国外的镜像,在下载的时候速度非常慢,下载速度是几kb或者几十kb,花费的时间比较长. 解决办法 国内目前有些机构或者公司整理了对应的镜像源,使得通过内网就能访问即可, ...

  7. go module基本使用

    前提 go版本为1.13及以上 官方文档 如果你想更深层次的了解GO MODULE的意义及开发者们的顾虑,可以直接访问官方文档(EN) https://github.com/golang/go/wik ...

  8. docker swarm CA证书到期

    1.现象 在portain平台查看日志,发现一些节点日志无法查看报错为:Error grabbing logs: rpc error: code = Unknown desc = warning: i ...

  9. [每日算法 - 阿里机试] leetcode19. 删除链表的倒数第 N 个结点 「 详细图释一看就懂!」

    入口 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer.https://le ...

  10. 国产化-内存数据库tendis-单机安装(完美替代redis)

    挺好的产品腾讯能开源还是体现了大厂的担当和格局,赞一个.阿里也开源了一些不错的产品后面讲. Tendis 介绍 Tendis 是腾讯公司开源的一款高性能分布式存储系统,基于 Redis 协议开发,具有 ...