126. Word Ladder II

这个题有点恶心的,思路和上一个差别不大,但是做起来好他妈麻烦。。

思路是先用BFS构图,Map<String, List>的adjacent list..
然后图来做dfs

和word ladder I 的区别在于,上一题我们只要找到层数就行了,比如1 - 2 - 3 - 4和 1- 3 - 2 - 4对我们来说答案都是4,返还就行,这里要都找出来。
所以不能直接在wordList上标记访问,否则会影响当层的判断结果,所以要以层为单位,再进入下一层的时候,一起加入访问的集合内。

然后还要避免重复,拿2-3来说,遍历2的时候会把3加入2的LIST,遍历3的时候又会把2加入3的list

public class Solution {
    public List<List<String>> findLadders(String beginWord, String endWord, Set<String> wordList) {
        List<List<String>> res = new ArrayList<>();
        if (beginWord.length() == 0 || endWord.length() == 0 || wordList.size() == 0) {
            return res;
        }
        Queue<String> q = new LinkedList<>();
        Map<String, List<String>> map = new HashMap<>();
        q.add(beginWord);
        boolean found = false;
        Set<String> totalVisit = new HashSet<>();
        Set<String> tempVisit = new HashSet<>();
        int left = 1;
        int total = 0;
        totalVisit.add(beginWord);
        while (!q.isEmpty()) {
            String s = q.poll();
            left--;
            for (int i = 0; i < s.length(); i++) {
                char[] arr = s.toCharArray();
                
                for (char j = 'a'; j <= 'z'; j++) {
                    arr[i] = j;
                    String tempS = String.valueOf(arr);
                    
                    if (tempS.equals(endWord)) {
                        found = true;
                        if (map.containsKey(s)) {
                            if(!map.get(s).contains(tempS)) {
                                map.get(s).add(tempS);
                            }
                        } else {
                            List<String> list = new ArrayList<>();
                            list.add(tempS);
                            map.put(s, list);
                        }
                        break;
                    }
                    if (wordList.contains(tempS) && !totalVisit.contains(tempS)) {
                        q.offer(tempS);
                        total++;
                        tempVisit.add(tempS);
                        if (map.containsKey(s)) {
                            if(!map.get(s).contains(tempS)) {
                                map.get(s).add(tempS);
                            }
                        } else {
                            List<String> list = new ArrayList<>();
                            list.add(tempS);
                            map.put(s, list);
                        }
                    }
                }
            }
            
            // finish one level
            if (left == 0) {
                if (found) break;
                left = total;
                total = 0;
                totalVisit.addAll(tempVisit);
                tempVisit.clear();
            }
            
            
            
        }
        List<String> tempList = new ArrayList<>();
        tempList.add(beginWord);
        dfs(res, map, tempList, beginWord, endWord);
        return res;
    }
    
    public void dfs(List<List<String>> res, Map<String,List<String>> map, List<String> tempList, String s, String endWord) {
        if (s.equals(endWord)) {
            res.add(new ArrayList<>(tempList));
            return;
        } else {
            List<String> tempLevel;
            if (map.containsKey(s)) {
                tempLevel = new ArrayList<>(map.get(s));
            } else {
                return;
            }
            for (int i = 0; i < tempLevel.size(); i++) {
                String tempS = tempLevel.get(i);
                tempList.add(tempS);
                dfs(res, map, tempList, tempS, endWord);
                tempList.remove(tempList.size() - 1);
            }
        }
    }
    

}

他妈的本来写的好好的,后来开始莫名其妙过不了了,开始改,照着YRB的改,改到最后和他一模一样还是过不了。。。报警了,忘了第一次怎么过的。然后顺手一点又他妈过了??什么情况。。代码一样的情况下还有一定几率过不了。。

然后test case:
input: NONE是什么鬼。。要求答案["a","c"]
完全不知道什么意思,这个TEST CASE不知道怎么过,又莫名其妙过了。。是lc崩了吗

哇呀呀~~ 我 ~ 生 ~ 气 ~ 啦~
image

贴的代码和我一开始写的在判断重复路径的方法上差别很大,而我忘了一开始怎么做的了,这个题做得我脑子炸了。
感觉被强奸了一样。。。
image

posted @ 2016-11-10 07:31  哇呀呀..生气啦~  阅读(150)  评论(0)    收藏  举报