麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

LintCode Topological Sorting

2019-11-10 20:22:07
字體:
來源:轉載
供稿:網友

description: Given an directed graph, a topological order of the graph nodes is defined as follow:

For each directed edge A -> B in graph, A must before B in the order list. The first node in the order can be any node in the graph with no nodes direct to it. Find any topological order for the given graph.

Notice

You can assume that there is at least one topological order in the graph.

Have you met this question in a real interview? Yes Clarification Learn more about rePResentation of graphs

Example For graph as follow:

picture

The topological order can be:

[0, 1, 2, 3, 4, 5] [0, 2, 3, 1, 5, 4] …

出現了一個問題,hashset是存入和取出是沒有規律的,但是這是有向圖的問題,因此因該使用arraylist來進行記錄

/** * Definition for Directed graph. * class DirectedGraphNode { * int label; * ArrayList<DirectedGraphNode> neighbors; * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); } * }; */public class Solution { /** * @param graph: A list of Directed graph node * @return: Any topological order for the given graph. */ public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) { // write your code here if (graph == null) { return null; } Map<DirectedGraphNode, Integer> map = new HashMap<>(); for (DirectedGraphNode node : graph) { for (DirectedGraphNode root : node.neighbors) { if (map.containsKey(root)) { map.put(root, map.get(root) + 1); } else { map.put(root, 1); } } } Queue<DirectedGraphNode> queue = new LinkedList<>(); ArrayList<DirectedGraphNode> set = new ArrayList<>(); for (DirectedGraphNode node : graph) { if(!map.containsKey(node)) { set.add(node); queue.offer(node); } } while (!queue.isEmpty()) { DirectedGraphNode root = queue.poll(); for (DirectedGraphNode node : root.neighbors) { map.put(node, map.get(node) - 1); if (map.get(node) == 0) { queue.offer(node); set.add(node); } } } return new ArrayList<DirectedGraphNode>(set); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美日韩在线播放 | 免费国产一级特黄久久 | 2021免费日韩视频网 | 亚洲精品欧美在线 | 91久久国产综合久久91精品网站 | 91av资源在线 | 精品国产一区二区三区久久久蜜月 | 99r国产精品 | 欧美一级不卡视频 | 中文字幕免费在线观看视频 | 欧美日本91精品久久久久 | 美女91视频 | 日韩美香港a一级毛片 | 亚洲国产美女视频 | 中文字幕精品在线观看 | 久久99国产精品视频 | 色阁阁69婷婷 | 日本黄色免费片 | 国产午夜亚洲精品理论片大丰影院 | 女18一级大黄毛片免费女人 | 激情网站在线观看 | 1314av | 亚洲3atv精品一区二区三区 | chinesehdxxxx无套 久久另类视频 | 在线播放av片 | 欧美成人一二三区 | 91九色论坛| 日本成人一二三区 | 久久人添人人爽人人爽人人片av | 国产毛片在线 | 欧美日本一 | 久久久久免费精品国产小说色大师 | 国产亚洲精品久久久久久久久 | 色婷婷久久久亚洲一区二区三区 | 7m视频成人精品分类 | 亚洲成人中文字幕在线 | www日韩大片| 羞羞的网址 | 在线亚洲播放 | 91成人免费网站 | 少妇色诱麻豆色哟哟 |