算法分析与设计课程(18):【leetcode】Clone Graph
来源:互联网 发布:日本食玩淘宝网 编辑:程序博客网 时间:2024/04/29 10:24
Description:
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbours.
OJ’s undirected graph serialization: Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label and each neighbour of the
node. As an example, consider the serialized graph {0,1,2#1,2#2,2}.
The graph has a total of three nodes, and therefore contains three parts as separated by #.
1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
2. Second node is labeled as 1. Connect node 1 to node 2.
3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1
/ \
/ \
0 --- 2
/ \
\_/
算法分析:
深度优先和广度优先都可以解决这个问题
代码如下:
DFS// LeetCode, Clone Graphclass Solution {public:UndirectedGraphNode *cloneGraph(const UndirectedGraphNode *node) {if(node == nullptr) return nullptr;// key is original node
- 算法分析与设计课程(18):【leetcode】Clone Graph
- 算法分析与设计课程(3):【leetcode】Permutations
- 算法分析与设计课程(4):【leetcode】Wildcard Matching
- 算法分析与设计课程(8):【leetcode】Sudoku Solver
- 算法分析与设计课程(9):【leetcode】Jump Game
- 算法分析与设计课程(10):【leetcode】Simplify Path
- 算法分析与设计课程(11):【leetcode】Gray Code
- 算法分析与设计课程(14):【leetcode】 Maximal Square
- 算法分析与设计课程(17):【leetcode】4Sum
- 算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph
- [week 18][Leetcode] Clone Graph
- LeetCode[Graph]: Clone Graph
- LeetCode(133) Clone Graph
- 算法分析与设计课程(5):【leetcode】Search for a Range
- 算法分析与设计课程(7):【leetcode】Reverse Nodes in k-Group
- 算法分析与设计课程(12):【leetcode】 Count Complete Tree Nodes
- 算法分析与设计课程(13):【leetcode】 Product of Array Except Self
- 算法分析与设计课程(15):【leetcode】 Find Minimum in Rotated Sorted Array
- centos5.5 安装tcpdump
- BF(Brute Force)算法
- Material Design之DrawerLayout与NavigationView实现抽屉效果
- 最小二乘法
- 网易评论盖楼的数据结构
- 算法分析与设计课程(18):【leetcode】Clone Graph
- mysql建库语句 最简单最常用语句
- 使用Ecs OpenAPI正确模式
- 将Excel表格的数据转换成sql的insert语句
- linux学习——索引文件
- mysql union all 排序无效
- 从JAVA字节码看++i 和i++ 的区别
- React Native 问题汇总
- Mysql安装过程中出现apply security settings错误的解决方法