<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    IT技術小屋

    秋風秋雨,皆入我心

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      38 隨筆 :: 1 文章 :: 19 評論 :: 0 Trackbacks
    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
    Only one letter can be changed at a time
    Each intermediate word must exist in the dictionary
    For example,
    Given:
    start = "hit"
    end = "cog"
    dict = ["hot","dot","dog","lot","log"]
    Return
      [
        ["hit","hot","dot","dog","cog"],
        ["hit","hot","lot","log","cog"]
      ]
    Note:
    All words have the same length.
    All words contain only lowercase alphabetic characters.

    這個題目應該算是leetcode上比較難的題目了。剛開始我采用了和Word Ladder相似的做法,只是用ArrayList記錄了當前變換路徑,在小數據的情況下可以Accept,但是大數據超時。究其原因,是由于為每個當前節(jié)點記錄變換路徑的時候,需要復制之前的ArrayList,這個時間開銷較大。
    其實,我們可以采用一個Map<String, HashSet<String>>結構,記錄字典單詞的每一個前驅,這樣我們可以從end反向遍歷,構造出轉換路徑。
    同時,我利用了兩個ArrayList,交替記錄上一層和下一層的節(jié)點,如果下一層節(jié)點為空,則不存在路徑,立即返回。如果下一層中出現了end,證明找到了所有的最短路徑,停止搜索開始構造路徑。實現代碼如下:
     1 public class WordLadderII {
     2     private void GeneratePath(Map<String, ArrayList<String>> prevMap,
     3             ArrayList<String> path, String word,
     4             ArrayList<ArrayList<String>> ret) {
     5         if (prevMap.get(word).size() == 0) {
     6             path.add(0, word);
     7             ArrayList<String> curPath = new ArrayList<String>(path);
     8             ret.add(curPath);
     9             path.remove(0);
    10             return;
    11         }
    12 
    13         path.add(0, word);
    14         for (String pt : prevMap.get(word)) {
    15             GeneratePath(prevMap, path, pt, ret);
    16         }
    17         path.remove(0);
    18     }
    19 
    20     public ArrayList<ArrayList<String>> findLadders(String start, String end,
    21             HashSet<String> dict) {
    22         ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
    23         Map<String, ArrayList<String>> prevMap = new HashMap<String, ArrayList<String>>();
    24         dict.add(start);
    25         dict.add(end);
    26         for (String d : dict) {
    27             prevMap.put(d, new ArrayList<String>());
    28         }
    29         ArrayList<HashSet<String>> candidates = new ArrayList<HashSet<String>>();
    30         candidates.add(new HashSet<String>());
    31         candidates.add(new HashSet<String>());
    32         int current = 0;
    33         int previous = 1;
    34         candidates.get(current).add(start);
    35         while (true) {
    36             current = current == 0 ? 1 : 0;
    37             previous = previous == 0 ? 1 : 0;
    38             for (String d : candidates.get(previous)) {
    39                 dict.remove(d);
    40             }
    41             candidates.get(current).clear();
    42             for (String wd : candidates.get(previous)) {
    43                 for (int pos = 0; pos < wd.length(); ++pos) {
    44                     StringBuffer word = new StringBuffer(wd);
    45                     for (int i = 'a'; i <= 'z'; ++i) {
    46                         if (wd.charAt(pos) == i) {
    47                             continue;
    48                         }
    49 
    50                         word.setCharAt(pos, (char) i);
    51 
    52                         if (dict.contains(word.toString())) {
    53                             prevMap.get(word.toString()).add(wd);
    54                             candidates.get(current).add(word.toString());
    55                         }
    56                     }
    57                 }
    58             }
    59 
    60             if (candidates.get(current).size() == 0) {
    61                 return ret;
    62             }
    63             if (candidates.get(current).contains(end)) {
    64                 break;
    65             }
    66         }
    67 
    68         ArrayList<String> path = new ArrayList<String>();
    69         GeneratePath(prevMap, path, end, ret);
    70 
    71         return ret;
    72     }
    73 }
    posted on 2014-01-02 13:59 Meng Lee 閱讀(865) 評論(0)  編輯  收藏 所屬分類: Leetcode
    主站蜘蛛池模板: 日本a级片免费看| 啦啦啦手机完整免费高清观看| 免费一级毛片不卡不收费| 波多野结衣亚洲一级| 国产成人福利免费视频| 亚洲高清中文字幕综合网| 亚洲w码欧洲s码免费| 亚洲综合视频在线观看| 在线观看www日本免费网站| 亚洲色图校园春色| 1000部羞羞禁止免费观看视频| 久久精品国产亚洲AV无码麻豆| 日韩在线永久免费播放| 老司机亚洲精品影院| 免费视频爱爱太爽了| 亚洲av无码国产综合专区| 免费毛片在线看片免费丝瓜视频| 亚洲av无码不卡久久| 破了亲妺妺的处免费视频国产| 亚洲av乱码一区二区三区按摩| 亚洲AV日韩精品一区二区三区| 特级毛片在线大全免费播放| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 亚洲国产午夜电影在线入口| 无人在线观看免费高清视频| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 | 狼群影院在线观看免费观看直播| 亚洲啪啪免费视频| 日本不卡在线观看免费v| fc2免费人成在线视频| 夜夜亚洲天天久久| 日韩av无码成人无码免费| 国产亚洲精彩视频| 亚洲AV无码一区二区二三区软件 | caoporn成人免费公开| 久久精品国产亚洲av日韩| 德国女人一级毛片免费| 中文字幕免费播放| 国产成人精品亚洲2020| 国产亚洲午夜高清国产拍精品 | 亚洲人成国产精品无码|