• Word Ladder II


    http://www.cnblogs.com/shawnhue/archive/2013/06/05/leetcode_126.html

    http://fisherlei.blogspot.com/2013/02/leetcode-word-ladder-ii-solution.html

    http://blog.csdn.net/niaokedaoren/article/details/8884938

     1 class Node {
     2     int no;
     3     String val;
     4     LinkedList<Node> prev;    
     5     
     6     Node(int no, String v) {
     7         this.no = no;
     8         this.val = v;
     9     }
    10     
    11     void addPrev(Node pNode) {
    12         if (prev == null) {
    13             prev = new LinkedList<Node>();
    14         }
    15         prev.add(pNode);
    16     }
    17 }
    18 
    19 public class Solution {    
    20     ArrayList<ArrayList<String>> answer;
    21     
    22     public void findPath(Node node, ArrayList<String> cur, String start) {
    23         if (node.val.equals(start)) {
    24             answer.add(cur);
    25             return;
    26         }
    27         ArrayList<String> temp;
    28         for (Node n : node.prev) {
    29             temp = new ArrayList<String>(cur);
    30             temp.add(0, n.val);
    31             findPath(n, temp, start);
    32         }
    33     }
    34     
    35     public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
    36         // Start typing your Java solution below
    37         // DO NOT write main() function
    38         HashMap<String, Node> map = new HashMap<String, Node>();
    39         Queue<Node> queue = new LinkedList<Node>();
    40         Node node = new Node(0, start);
    41         Node endNode = null;
    42         map.put(start, node);
    43         queue.add(node);
    44         boolean stop = false;
    45         while (queue.size() > 0 && !stop) {
    46             int count = queue.size();
    47             for (int i = 0; i < count; i++) {
    48                 node = queue.poll();
    49                 for (int j = 0; j < node.val.length(); j++) {
    50                     StringBuilder t = new StringBuilder(node.val);
    51                     for (char k = 'a'; k <= 'z'; k++) {
    52                         t.setCharAt(j, k);
    53                         if (dict.contains(t.toString())) {
    54                             Node v = map.get(t.toString());
    55                             if (v == null) {
    56                                 Node temp = new Node(node.no + 1, t.toString());
    57                                 temp.addPrev(node);
    58                                 queue.add(temp);
    59                                 map.put(t.toString(), temp);
    60                                 if (t.toString().equals(end)) {
    61                                     endNode = temp;
    62                                     stop = true;
    63                                 }
    64                             }
    65                             else {
    66                                 if (v.no == node.no + 1) {
    67                                     v.addPrev(node);
    68                                 }
    69                             }
    70                         }
    71                     }
    72                 }
    73             }
    74         }
    75         answer = new ArrayList<ArrayList<String>>();
    76         if (endNode != null) {
    77             findPath(endNode, new ArrayList<String>(Arrays.asList(end)), start);
    78         }
    79         return answer;
    80     }
    81 }
  • 相关阅读:
    大数据Hadoop第二周——配置新的节点DataNode及ip地址
    vue环境搭建详细步骤
    苹果电脑Mac系统如何下载安装谷歌Chrome浏览器
    点云的基本特征和描述
    ModuleNotFoundError: No module named 'rospkg'
    ROS的多传感器时间同步机制Time Synchronizer
    Spring Cloud 2020 版本重大变革,更好的命名方式!
    Spring MVC 接收请求参数所有方式总结!
    阿里为什么不用 Zookeeper 做服务发现?
    微服务之间最佳调用方式是什么?
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537175.html
Copyright © 2020-2023  润新知