leetcode Clone Graph
来源:互联网 发布:e63诺基亚软件下载网址 编辑:程序博客网 时间:2024/06/16 17:35
题目链接
这个题目就是用深度优先遍历就好。但是记录方式要更改一下。并不是那个点已经访问过就直接返回。因为一个点可以重复链接自己,也可重复链接别人
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */public class Solution { HashMap<Integer,UndirectedGraphNode> record=new HashMap<>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node==null) { return node; } UndirectedGraphNode result=new UndirectedGraphNode(node.label); record.put(node.label,result); for(UndirectedGraphNode niber:node.neighbors) { help(result,niber); } return result; } public void help(UndirectedGraphNode father,UndirectedGraphNode ori) { UndirectedGraphNode newNode=record.get(ori.label); if(newNode!=null) { father.neighbors.add(newNode); return; } newNode=new UndirectedGraphNode(ori.label); record.put(ori.label,newNode); father.neighbors.add(newNode); for(UndirectedGraphNode niber:ori.neighbors) { help(newNode,niber); } }}
0 0
- LeetCode[Graph]: Clone Graph
- [LeetCode] CLone Graph
- LeetCode:Clone Graph
- [leetcode]Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- LeetCode题解:Clone Graph
- [LeetCode]Clone Graph
- leetcode Clone Graph
- leetcode-Clone Graph
- LeetCode | Clone Graph
- Leetcode: Clone Graph
- [LeetCode] Clone Graph
- LeetCode OJ:Clone Graph
- Leetcode Clone Graph
- [Leetcode] Clone Graph (Java)
- Clone Graph - LeetCode
- 【leetcode】Clone Graph
- ViewInject原理
- 死锁的产生以及如何防止死锁的产生
- trickyPriorityQueue
- python 装饰器传参
- [leetcode] 【链表】 146. LRU Cache
- leetcode Clone Graph
- hdu2602-01背包
- java 反射机制
- TimesTen 数据库复制学习:4. 定义Active Standby Pair复制策略
- Shadertoy编程学习(1):第一个shader程序
- 整理的最新WebSHell (php过狗一句话,过狗菜刀,2016过狗一句话,2016php免杀一句话)
- 一切都是代名词
- JSP基础语法和相关用法
- LTP在vs下的配置方法