LeetCode——Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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.

» Solve this problem


写的有点烦。

题目本身倒是不难,主要是记录路径,然后还原。


class Node {
    int no;
    String val;
    LinkedList<Node> prev;    
    
    Node(int no, String v) {
        this.no = no;
        this.val = v;
    }
    
    void addPrev(Node pNode) {
        if (prev == null) {
            prev = new LinkedList<Node>();
        }
        prev.add(pNode);
    }
}

public class Solution {    
    ArrayList<ArrayList<String>> answer;
    
    public void findPath(Node node, ArrayList<String> cur, String start) {
        if (node.val.equals(start)) {
            answer.add(cur);
            return;
        }
        ArrayList<String> temp;
        for (Node n : node.prev) {
            temp = new ArrayList<String>(cur);
            temp.add(0, n.val);
            findPath(n, temp, start);
        }
    }
    
    public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        HashMap<String, Node> map = new HashMap<String, Node>();
        Queue<Node> queue = new LinkedList<Node>();
        Node node = new Node(0, start);
        Node endNode = null;
        map.put(start, node);
        queue.add(node);
        boolean stop = false;
        while (queue.size() > 0 && !stop) {
            int count = queue.size();
            for (int i = 0; i < count; i++) {
                node = queue.poll();
                for (int j = 0; j < node.val.length(); j++) {
                    StringBuilder t = new StringBuilder(node.val);
                    for (char k = 'a'; k <= 'z'; k++) {
                        t.setCharAt(j, k);
                        if (dict.contains(t.toString())) {
                            Node v = map.get(t.toString());
                            if (v == null) {
                                Node temp = new Node(node.no + 1, t.toString());
                                temp.addPrev(node);
                                queue.add(temp);
                                map.put(t.toString(), temp);
                                if (t.toString().equals(end)) {
                                    endNode = temp;
                                    stop = true;
                                }
                            }
                            else {
                                if (v.no == node.no + 1) {
                                    v.addPrev(node);
                                }
                            }
                        }
                    }
                }
            }
        }
        answer = new ArrayList<ArrayList<String>>();
        if (endNode != null) {
            findPath(endNode, new ArrayList<String>(Arrays.asList(end)), start);
        }
        return answer;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值