[Leetcode] Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence 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"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.
    • All words contain only lowercase alphabetic characters.

 

Solution:

我自己的做法,大数据时会超时:

利用两个queue:

1个queue用来存放本层的单词,一个queue用来存放下一层的单词。

当本层的单词用完了以后,再将下一层的单词变成本层的就好,下一层清空。

注意,java的object是pass by reference的,得重新new一下才行。

 1 public int ladderLength(String start, String end, Set<String> dict) {
 2         if(start==null||end==null||dict==null||dict.size()==0){
 3             return 0;
 4         }
 5         Queue<String> curQueue=new LinkedList<String>();
 6         Queue<String> nextQueue=new LinkedList<String>();
 7         curQueue.offer(start);
 8         int count=1;
 9         if(dict.contains(start))
10             dict.remove(start);
11         while(!curQueue.isEmpty()){              
12             if(curQueue.peek().equals(end))
13                 return count;
14             String haha=curQueue.poll();            
15             for(int i=0;i<start.length();++i){
16                 char[] temp_c=haha.toCharArray();
17                 char m=temp_c[i];
18                 for(char j='a';j<='z';++j){
19                     if(m==j)
20                         continue;
21                     temp_c[i]=j;
22                     String temp=new String(temp_c);  
23                     //System.out.println("size o Cur Queue==="+temp);
24                     if(dict.contains(temp)){
25                         
26                         dict.remove(temp);
27                         nextQueue.offer(temp);
28                     }
29                 }
30             }
31             if(curQueue.isEmpty()){
32                 curQueue=new LinkedList<String>(nextQueue);
33                 nextQueue.clear();
34                 count++;
35             }
36         }
37         return 0;
38     }

---------------------------------------------------------------------------

网上看到的做法:

不用两个queue来分别存放上下两层的数据,只用一个queue来实现就好,但是分别用两个counter来计数,上层有多少个words,下层有多少个words。同理,当上层的words都从queue里poll出去了以后,上层counter=下层counter,下层counter清空。

 1 public class Solution {
 2     public int ladderLength(String start, String end, Set<String> dict) {
 3         if(start==null||end==null||dict==null||dict.size()==0)
 4             return 0;
 5         Queue<String> queue=new LinkedList<String>();
 6         queue.offer(start);
 7         int curNum=1;
 8         int nextNum=0;
 9         int level=1;
10         while(!queue.isEmpty()){
11             if(queue.peek().equals(end)){
12                 return level;
13             }
14             curNum--;
15             String temp=queue.poll();
16             for(int i=0;i<temp.length();++i){
17                 char[] c=temp.toCharArray();
18                 for(char j='a';j<='z';++j){
19                     c[i]=j;
20                     String sc=new String(c);
21                     // if(sc.equals(end)){
22                     //     return level;
23                     // }
24                     if(dict.contains(sc)){
25                         dict.remove(sc);
26                         nextNum++;
27                         queue.offer(sc);
28                     }
29                 }
30             }
31             if(curNum==0){
32                 curNum=nextNum;
33                 nextNum=0;
34                 level++;
35             }
36         }
37         return 0;
38     }
39 }

 

posted @ 2015-02-01 08:39  Phoebe815  阅读(173)  评论(0编辑  收藏  举报