leetcode: Clone Graph
来源:互联网 发布:网络购票实名认证 编辑:程序博客网 时间:2024/05/17 20:02
还是一道要求完全克隆的题目.....重点是要将所有点找出并克隆病完整保存点和点间的关系
思路一:实现找出并新建立所有的点存入map,之后再次遍历建立关系
看了下网上的方法,思路二很好.....用map保存新建立即已克隆的点...从node开始逐个点克隆...若该点已克隆,则直接取出克隆点,否则克隆...之后要判断该点的邻点继续克隆直到结束
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * List<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } * }; */public class Solution { Map<Integer,UndirectedGraphNode> nodes = new HashMap<Integer,UndirectedGraphNode>(); public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { return clone(node); } UndirectedGraphNode clone(UndirectedGraphNode node){ if(node == null) { return null; } if(nodes.containsKey(node.label)) { return nodes.get(node.label); } UndirectedGraphNode tempNode = new UndirectedGraphNode(node.label); nodes.put(node.label,tempNode); for(UndirectedGraphNode n : node.neighbors) { UndirectedGraphNode Newn = clone(n); tempNode.neighbors.add(Newn); } return tempNode; }}
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
- 面试经验
- Android应用底部导航栏(选项卡)实例
- iOS Documents、Library和tmp的作用
- error:Dreamweaver总提示语法错误,我以为Dreamweaver不识别语法错误,后来才知道真是我错了
- Leetcode dfs Construct Binary Tree from Preorder and Inorder Traversal
- leetcode: Clone Graph
- Cocos2d-x 3.x开发——导入Cocostudio资源
- 基础4
- Mongodb如何导入导出数据
- BASE64转换,内含:IOS自带DES加解密
- acdream1188 Read Phone Number 水题
- 如何在Ubuntu Unity上修改应用程序图标
- android银联支付
- IPicture总结