word ladder

超时

public class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        //You may assume beginWord and endWord are non-empty and are not the same.
        if (wordList == null) {
            return 0;
        }
        int length = 2;
        
        Queue<String> queue = new LinkedList<>();
        Set<String> hash = new HashSet<>();
        
        queue.add(beginWord);
        hash.add(beginWord);
        
        boolean find = false;
        while (!queue.isEmpty()) {
            int size = queue.size();
            //System.out.println("length" + length);
            for (int k = 0; k < size; k++) {
                String node = queue.poll();
                //System.out.println("一层" + node);
                if (node.equals(endWord)) {
                    find = true;
                    return ++length;
                    
                }
                for (int i = 0; i < wordList.size(); i++) {
                    int count = 0;
                    String next = wordList.get(i);
                    
                    if (!hash.contains(next)) {
                    //System.out.print("next");
                    //System.out.println(next);
                        for (int j = 0; j < node.length(); j++) {
                            if (! (node.substring(j, j + 1).equals(next.substring(j, j + 1)))) {
                                count++;
                            }
                        } 
                        if (count == 1) {
                            if (next.equals(endWord)) {
                                return length;
                            }
                            queue.add(next);
                            hash.add(next);
                            //System.out.print("add");
                            //System.out.println(next);
                        }
                    }
                }
                
            }
            length++;
        }
        return 0;
    }
}
View Code

 果然需要dic,参照答案思路写的,效率不高,不知道为啥

public class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        if (wordList == null || wordList.size() == 0) {
            return 0;
        }
        //dict
        Set<String> dict = new HashSet<>();
        for (String s : wordList) {
            dict.add(s);
        }
        
        Queue<String> queue = new LinkedList<>();
        Set<String> hash = new HashSet<>();
        queue.offer(beginWord);
        hash.add(beginWord);
        int length = 1;
        while (!queue.isEmpty()) {
            length++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                String node = queue.poll();
                //for循环每一个字母
                for (String next : getNextWord(node, hash, dict)) {
                    if (next.equals(endWord)) {
                        return length;
                    }
                    queue.offer(next);
                    hash.add(next);
                }
            }
        }
        return 0;
    }
    private List<String> getNextWord(String node, Set<String> hash, Set<String> dict) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < node.length(); i++) {
            for (char j = 'a'; j <= 'z'; j++) {
                String next = replace(node, i, j);
                if (dict.contains(next) && !hash.contains(next)) {
                    result.add(next);
                }
            }
        }
        return result;
    }
    private String replace(String s, int index, char c) {
        char[] chars = s.toCharArray();
        chars[index] = c;
        return new String(chars); //需要deep copy
    }
}
View Code

重点:dict加入所有word。

  三重循环  其中加入size这层,每层length++

  char[] chars = string.toCharArray();

  string.charAt(i);

  deep copy;

posted @ 2017-06-20 09:08  yunyouhua  阅读(112)  评论(0编辑  收藏  举报