Leetcode刷题系列(十七)Graph相关
来源:互联网 发布:上海ug编程培训021ug 编辑:程序博客网 时间:2024/05/17 20:43
本篇给出几道关于图的问题。但是没有深度搜索和广度搜索的题型,仅是与图结构相关的。
Clone Graph
这道题为深度克隆一张图。我们分点和点的对应关系分别进行克隆。
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) { return node; } // use bfs algorithm to traverse the graph and get all nodes. ArrayList<UndirectedGraphNode> nodes = getNodes(node); // copy nodes, store the old->new mapping information in a hash map HashMap<UndirectedGraphNode, UndirectedGraphNode> mapping = new HashMap<>(); for (UndirectedGraphNode n : nodes) { mapping.put(n, new UndirectedGraphNode(n.label)); } // copy neighbors(edges) for (UndirectedGraphNode n : nodes) { UndirectedGraphNode newNode = mapping.get(n); for (UndirectedGraphNode neighbor : n.neighbors) { UndirectedGraphNode newNeighbor = mapping.get(neighbor); newNode.neighbors.add(newNeighbor); } } return mapping.get(node);}private ArrayList<UndirectedGraphNode> getNodes(UndirectedGraphNode node) { Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); HashSet<UndirectedGraphNode> set = new HashSet<>(); queue.offer(node); set.add(node); while (!queue.isEmpty()) { UndirectedGraphNode head = queue.poll(); for (UndirectedGraphNode neighbor : head.neighbors) { if(!set.contains(neighbor)){ set.add(neighbor); queue.offer(neighbor); } } } return new ArrayList<UndirectedGraphNode>(set);}
Copy List with Random Pointer
这道题是深度克隆一个链表,链表上的节点带随机指针。与上道题相似,先克隆节点再克隆关系。
public RandomListNode copyRandomList(RandomListNode head) { if (head == null) { return null; } HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>(); RandomListNode dummy = new RandomListNode(0); RandomListNode pre = dummy, newNode; while (head != null) { if (map.containsKey(head)) { newNode = map.get(head); } else { newNode = new RandomListNode(head.label); map.put(head, newNode); } pre.next = newNode; if (head.random != null) { if (map.containsKey(head.random)) { newNode.random = map.get(head.random); } else { newNode.random = new RandomListNode(head.random.label); map.put(head.random, newNode.random); } } pre = newNode; head = head.next; } return dummy.next;}
Topological Sorting
这道题是有序图的拓扑排序的标准算法,理解拓扑排序即可理解下面代码的过程。
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) { // write your code here ArrayList<DirectedGraphNode> result = new ArrayList<DirectedGraphNode>(); HashMap<DirectedGraphNode, Integer> map = new HashMap(); for (DirectedGraphNode node : graph) { for (DirectedGraphNode neighbor : node.neighbors) { if (map.containsKey(neighbor)) { map.put(neighbor, map.get(neighbor) + 1); } else { map.put(neighbor, 1); } } } Queue<DirectedGraphNode> q = new LinkedList<DirectedGraphNode>(); for (DirectedGraphNode node : graph) { if (!map.containsKey(node)) { q.offer(node); result.add(node); } } while (!q.isEmpty()) { DirectedGraphNode node = q.poll(); for (DirectedGraphNode n : node.neighbors) { map.put(n, map.get(n) - 1); if (map.get(n) == 0) { result.add(n); q.offer(n); } } } return result;}
0 0
- Leetcode刷题系列(十七)Graph相关
- Leetcode刷题系列(三)Rotated Array相关
- Leetcode刷题系列(四)Binary Tree相关
- Leetcode刷题系列(五)Binary Search Tree相关
- Sum Root to Leaf Numbers ——Leetcode系列(十七)
- leetcode第一刷_Clone Graph
- LeetCode(133) Clone Graph
- 算法题练习系列之(十七): 科学计数法
- 算法题练习系列之(二十七): 有理数四则运算
- 操作系统刷题(十七)
- LeetCode[Graph]: Clone Graph
- LeetCode OJ算法题(十七):4Sum
- LeetCode OJ算法题(二十七):Implement strStr()
- LeetCode:Graph,
- LeetCode刷题系列
- C++编程入门系列之二十七(数组、指针和字符串:指针变量的声明、地址相关运算--“*”和“&”)
- Html5系列(十七) canvas 对角线渐变
- javaWeb系列之十七(上传下载)
- 【树的重心】 POJ 1655 Balancing Act
- HDU 1829 A Bug's Life(基础种类并查集)
- LeetCode刷题系列(十六)Some Little Questions
- 日本国立国会图书馆书籍批量下载(一)
- java新手学习之开发教程
- Leetcode刷题系列(十七)Graph相关
- java语言编程简介
- Leetcode刷题系列(十八)CombinationSum && WordLadder
- 编译的细分————
- 万能Host google
- Spark 应用程序调优
- java环境变量设置
- 信息系统项目管理师--47分论文--范围管理
- microstation level3 07 Set auxiliary coordinates for cylinder