📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2566| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] 【LintCode一日一问】Topological Sorting我的这个答案有错吗?

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 小马3107 于 2015-8-14 12:27 编辑

啥都不说先上图。这道题错的挺奇怪的。给了输入是
  1. {0,1,2,3,4#1,3,4#2,1,4#3,4#4}
复制代码
我的输出是
  1. [0,1,2,3,4]
复制代码
按说也没问题,但答案提示我 “You only need to return any topological order for the given graph.” 这个提示好奇怪,又开始陷入痛苦的思索中。我的[0,1,2,3,4]也算是正确答案之一吧?以下是代码和注释:
  1. public static ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
  2.                 if (graph == null){
  3.                     return null; //特殊值
  4.                 }
  5.                 if (graph.size() == 1){
  6.                     return graph;//特殊值
  7.                 }
  8.                 Set<DirectedGraphNode> neighborSet = new HashSet<DirectedGraphNode>();
  9.                 for(DirectedGraphNode node : graph){
  10.                     neighborSet.addAll(node.neighbors);//将所有有in-degree的点放进集合
  11.                 }
  12.                 ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();
  13.                 for (DirectedGraphNode node : graph){
  14.                     if (!neighborSet.contains(node)){
  15.                         res.add(node);//找到初始的点,即只出不入的点
  16.                     }
  17.                 }
  18.                 ArrayList<DirectedGraphNode> finalres = new ArrayList<DirectedGraphNode>();
  19.                 while (!res.isEmpty()){
  20.                     DirectedGraphNode node = res.get(0);
  21.                     res.remove(0);
  22.                     res.addAll(node.neighbors);//开始BFS
  23.                     if(!finalres.contains(node)){
  24.                             finalres.add(node);
  25.                     }
  26.                 }
  27.                 return finalres;
  28.             }
复制代码
求拍醒!!

1.png (77.08 KB, 下载次数: 0)

1.png

上一篇:cc 150 6.5
下一篇:如果hashcode不是O(1),那么hashmap能保证O(1)look up吗?
推荐
 楼主| 小马3107 2015-8-19 22:04:53 | 只看该作者
全局:
嗯经过仔细整理以后把我原来的代码修改了一下以后提交通过了。谢谢大家!
  1. public static ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
  2.                 if (graph == null){
  3.                     return null;
  4.                 }
  5.                 if (graph.size() == 1){
  6.                     return graph;
  7.                 }
  8.                 Set<DirectedGraphNode> neighborSet = new HashSet<DirectedGraphNode>();
  9.                 for(DirectedGraphNode node : graph){
  10.                     neighborSet.addAll(node.neighbors);
  11.                 }
  12.                 ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();
  13.                 ArrayList<DirectedGraphNode> copyGraph = new ArrayList<DirectedGraphNode>();
  14.                 copyGraph.addAll(graph);
  15.                 for (DirectedGraphNode node : graph){
  16.                     if (!neighborSet.contains(node)){
  17.                         res.add(node);
  18.                         copyGraph.remove(node);
  19.                     }
  20.                 }
  21.                 ArrayList<DirectedGraphNode> remaining = topSort(copyGraph);
  22.                 res.addAll(remaining);
  23.                 return res;
  24.             }
复制代码
回复

使用道具 举报

推荐
stellari 2015-8-14 18:25:39 | 只看该作者
全局:
你的解显然不对。原图中有2,1,4这一部分,说明1或4都在2的后面(或都在前面,这取决于这个图是parent的排序靠前还是offspring的排序靠前)。所以答案应该是[0, 2, 1, 3, 4]或者是[4, 3, 1, 2, 0];无论如何不可能[0, 1 2 3 4]
回复

使用道具 举报

🔗
 楼主| 小马3107 2015-8-14 18:51:19 | 只看该作者
全局:
stellari 发表于 2015-8-14 18:25
你的解显然不对。原图中有2,1,4这一部分,说明1或4都在2的后面(或都在前面,这取决于这个图是parent的排 ...

是嘛我一直以为既然0同时连接了1和2所以1和2的顺序是没关系的只要排在0后面就行。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表