查看: 3981| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] 个人感觉思路比较清晰的word ladder II解法 by Java, 欢迎大家来讨论其他更好方法

全局:

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

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

x
  1. public class Solution {
  2.     public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
  3.     // Start typing your Java solution below
  4.     // DO NOT write main() function
  5.     // method would be similar, except that, we need to store the path
  6.     // in addition, if we get the result, we didn't stop until we finish
  7.     // all the path of the same length
  8.     // visited map the string to the list of its ancestor
  9.     HashMap<String, HashSet<String>> visited=new HashMap<String, HashSet<String>>();
  10.     HashMap<String, Integer> level=new HashMap<String, Integer>();
  11.     LinkedList<String> queue=new LinkedList<String>();
  12.     ArrayList<ArrayList<String>> result=new ArrayList<ArrayList<String>>();
  13.     if (start==null || end==null || start.length()!=end.length())
  14.     {
  15.         return result;
  16.     }
  17.     // we also need to store the path for the start
  18.     HashSet<String> path=new HashSet<String>();
  19.     // we record the minimal length we get
  20.     int min_length=Integer.MAX_VALUE;
  21.     visited.put(start, path);
  22.     level.put(start, 1);
  23.     queue.add(start);
  24.     while(!queue.isEmpty())
  25.     {
  26.         String s=queue.remove();
  27.         char[] chars=s.toCharArray();
  28.         for (int i=0; i<s.length(); i++)
  29.         {
  30.             char old=chars[i];
  31.             for (char c='a'; c<='z'; c++)
  32.             {
  33.                 chars[i]=c;
  34.                 String s2=new String(chars);
  35.                 // avoid circle
  36.                 // check whether it is in the dictionary
  37.                 // we only add the string which is nearer to the start
  38.                 if (dict.contains(s2) && (!level.containsKey(s2) || (level.containsKey(s2) && level.get(s2)>level.get(s))))
  39.                 {
  40.                     // we update the ancestor of the string
  41.                     if (visited.containsKey(s2))
  42.                     {
  43.                         visited.get(s2).add(s);
  44.                     }
  45.                     else
  46.                     {
  47.                         // we haven't seen this node before
  48.                         // thus we add it to the queue and also its ancestor
  49.                         path=new HashSet<String>();
  50.                         path.add(s);
  51.                         visited.put(s2, path);
  52.                         level.put(s2, level.get(s)+1);
  53.                         queue.add(s2);
  54.                     }
  55.                 }
  56.                 if (s2.equals(end))
  57.                 {
  58.                     // we found it
  59.                     // we will use back trace to found its path to start
  60.                     if (level.get(s)<min_length)
  61.                     {
  62.                         // it is shortest path
  63.                         ArrayList<String> entry=new ArrayList<String>();
  64.                         entry.add(end);
  65.                         result.addAll(back_trace(s, visited, entry));
  66.                         min_length=level.get(s)+1;
  67.                     }
  68.                     else
  69.                     {
  70.                         // ok, all the remaining path should be longer
  71.                         break;
  72.                     }
  73.                 }
  74.             }
  75.             chars[i]=old;
  76.         }
  77.     }
  78.     return result;
  79. }

  80. private ArrayList<ArrayList<String>> back_trace(String end, HashMap<String, HashSet<String>> visited, ArrayList<String> path)
  81. {
  82.     ArrayList<ArrayList<String>> result=new ArrayList<ArrayList<String>>();
  83.     ArrayList<String> entry=new ArrayList<String>(path);
  84.     entry.add(0, end);
  85.     if (visited.get(end).size()<1)
  86.     {
  87.         result.add(entry);
  88.         return result;
  89.     }
  90.     for (String str: visited.get(end))
  91.     {
  92.         result.addAll(back_trace(str, visited, entry));
  93.     }
  94.     return result;
  95. }
  96. }
复制代码

上一篇:刚下了dommy的leetcode,但是java文件里面中文注释是乱码
下一篇:不太理解 BST的remove的code
🔗
readman 2014-11-9 07:57:59 | 只看该作者
全局:
速度还不错. 刚试了试..思路嘛..不是原创吧..以前见过
回复

使用道具 举报

🔗
toplee_xd 2014-11-21 18:03:39 | 只看该作者
全局:
如果你 写一个 graph 的类来做这题更加清楚易懂。。。。这题考察的关键就是graph search。。
回复

使用道具 举报

🔗
忆梦前尘 2016-2-22 12:54:05 | 只看该作者
全局:
这道题有没有文字讲解版本的。。
回复

使用道具 举报

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

本版积分规则

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